1 //===-- ClangExpressionSourceCode.cpp ---------------------------*- C++ -*-===//
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 "ClangExpressionSourceCode.h"
10 
11 #include "clang/Basic/CharInfo.h"
12 #include "llvm/ADT/StringRef.h"
13 
14 #include "Plugins/ExpressionParser/Clang/ClangModulesDeclVendor.h"
15 #include "Plugins/ExpressionParser/Clang/ClangPersistentVariables.h"
16 #include "lldb/Symbol/Block.h"
17 #include "lldb/Symbol/CompileUnit.h"
18 #include "lldb/Symbol/DebugMacros.h"
19 #include "lldb/Symbol/TypeSystem.h"
20 #include "lldb/Symbol/VariableList.h"
21 #include "lldb/Target/ExecutionContext.h"
22 #include "lldb/Target/Language.h"
23 #include "lldb/Target/Platform.h"
24 #include "lldb/Target/StackFrame.h"
25 #include "lldb/Target/Target.h"
26 #include "lldb/Utility/StreamString.h"
27 
28 using namespace lldb_private;
29 
30 const char *ClangExpressionSourceCode::g_expression_prefix = R"(
31 #ifndef NULL
32 #define NULL (__null)
33 #endif
34 #ifndef Nil
35 #define Nil (__null)
36 #endif
37 #ifndef nil
38 #define nil (__null)
39 #endif
40 #ifndef YES
41 #define YES ((BOOL)1)
42 #endif
43 #ifndef NO
44 #define NO ((BOOL)0)
45 #endif
46 typedef __INT8_TYPE__ int8_t;
47 typedef __UINT8_TYPE__ uint8_t;
48 typedef __INT16_TYPE__ int16_t;
49 typedef __UINT16_TYPE__ uint16_t;
50 typedef __INT32_TYPE__ int32_t;
51 typedef __UINT32_TYPE__ uint32_t;
52 typedef __INT64_TYPE__ int64_t;
53 typedef __UINT64_TYPE__ uint64_t;
54 typedef __INTPTR_TYPE__ intptr_t;
55 typedef __UINTPTR_TYPE__ uintptr_t;
56 typedef __SIZE_TYPE__ size_t;
57 typedef __PTRDIFF_TYPE__ ptrdiff_t;
58 typedef unsigned short unichar;
59 extern "C"
60 {
61     int printf(const char * __restrict, ...);
62 }
63 )";
64 
65 static const char *c_start_marker = "    /*LLDB_BODY_START*/\n    ";
66 static const char *c_end_marker = ";\n    /*LLDB_BODY_END*/\n";
67 
68 namespace {
69 
70 class AddMacroState {
71   enum State {
72     CURRENT_FILE_NOT_YET_PUSHED,
73     CURRENT_FILE_PUSHED,
74     CURRENT_FILE_POPPED
75   };
76 
77 public:
78   AddMacroState(const FileSpec &current_file, const uint32_t current_file_line)
79       : m_state(CURRENT_FILE_NOT_YET_PUSHED), m_current_file(current_file),
80         m_current_file_line(current_file_line) {}
81 
82   void StartFile(const FileSpec &file) {
83     m_file_stack.push_back(file);
84     if (file == m_current_file)
85       m_state = CURRENT_FILE_PUSHED;
86   }
87 
88   void EndFile() {
89     if (m_file_stack.size() == 0)
90       return;
91 
92     FileSpec old_top = m_file_stack.back();
93     m_file_stack.pop_back();
94     if (old_top == m_current_file)
95       m_state = CURRENT_FILE_POPPED;
96   }
97 
98   // An entry is valid if it occurs before the current line in the current
99   // file.
100   bool IsValidEntry(uint32_t line) {
101     switch (m_state) {
102     case CURRENT_FILE_NOT_YET_PUSHED:
103       return true;
104     case CURRENT_FILE_PUSHED:
105       // If we are in file included in the current file, the entry should be
106       // added.
107       if (m_file_stack.back() != m_current_file)
108         return true;
109 
110       return line < m_current_file_line;
111     default:
112       return false;
113     }
114   }
115 
116 private:
117   std::vector<FileSpec> m_file_stack;
118   State m_state;
119   FileSpec m_current_file;
120   uint32_t m_current_file_line;
121 };
122 
123 } // anonymous namespace
124 
125 static void AddMacros(const DebugMacros *dm, CompileUnit *comp_unit,
126                       AddMacroState &state, StreamString &stream) {
127   if (dm == nullptr)
128     return;
129 
130   for (size_t i = 0; i < dm->GetNumMacroEntries(); i++) {
131     const DebugMacroEntry &entry = dm->GetMacroEntryAtIndex(i);
132     uint32_t line;
133 
134     switch (entry.GetType()) {
135     case DebugMacroEntry::DEFINE:
136       if (state.IsValidEntry(entry.GetLineNumber()))
137         stream.Printf("#define %s\n", entry.GetMacroString().AsCString());
138       else
139         return;
140       break;
141     case DebugMacroEntry::UNDEF:
142       if (state.IsValidEntry(entry.GetLineNumber()))
143         stream.Printf("#undef %s\n", entry.GetMacroString().AsCString());
144       else
145         return;
146       break;
147     case DebugMacroEntry::START_FILE:
148       line = entry.GetLineNumber();
149       if (state.IsValidEntry(line))
150         state.StartFile(entry.GetFileSpec(comp_unit));
151       else
152         return;
153       break;
154     case DebugMacroEntry::END_FILE:
155       state.EndFile();
156       break;
157     case DebugMacroEntry::INDIRECT:
158       AddMacros(entry.GetIndirectDebugMacros(), comp_unit, state, stream);
159       break;
160     default:
161       // This is an unknown/invalid entry. Ignore.
162       break;
163     }
164   }
165 }
166 
167 /// Checks if the expression body contains the given variable as a token.
168 /// \param body The expression body.
169 /// \param var The variable token we are looking for.
170 /// \return True iff the expression body containes the variable as a token.
171 static bool ExprBodyContainsVar(llvm::StringRef body, llvm::StringRef var) {
172   assert(var.find_if([](char c) { return !clang::isIdentifierBody(c); }) ==
173              llvm::StringRef::npos &&
174          "variable contains non-identifier chars?");
175 
176   size_t start = 0;
177   // Iterate over all occurences of the variable string in our expression.
178   while ((start = body.find(var, start)) != llvm::StringRef::npos) {
179     // We found our variable name in the expression. Check that the token
180     // that contains our needle is equal to our variable and not just contains
181     // the character sequence by accident.
182     // Prevents situations where we for example inlcude the variable 'FOO' in an
183     // expression like 'FOObar + 1'.
184     bool has_characters_before =
185         start != 0 && clang::isIdentifierBody(body[start - 1]);
186     bool has_characters_after =
187         start + var.size() < body.size() &&
188         clang::isIdentifierBody(body[start + var.size()]);
189 
190     // Our token just contained the variable name as a substring. Continue
191     // searching the rest of the expression.
192     if (has_characters_before || has_characters_after) {
193       ++start;
194       continue;
195     }
196     return true;
197   }
198   return false;
199 }
200 
201 static void AddLocalVariableDecls(const lldb::VariableListSP &var_list_sp,
202                                   StreamString &stream,
203                                   const std::string &expr) {
204   for (size_t i = 0; i < var_list_sp->GetSize(); i++) {
205     lldb::VariableSP var_sp = var_list_sp->GetVariableAtIndex(i);
206 
207     ConstString var_name = var_sp->GetName();
208     if (!var_name || var_name == "this" || var_name == ".block_descriptor")
209       continue;
210 
211     if (!expr.empty() && !ExprBodyContainsVar(expr, var_name.GetStringRef()))
212       continue;
213 
214     stream.Printf("using $__lldb_local_vars::%s;\n", var_name.AsCString());
215   }
216 }
217 
218 bool ClangExpressionSourceCode::GetText(
219     std::string &text, lldb::LanguageType wrapping_language, bool static_method,
220     ExecutionContext &exe_ctx, bool add_locals, bool force_add_all_locals,
221     llvm::ArrayRef<std::string> modules) const {
222   const char *target_specific_defines = "typedef signed char BOOL;\n";
223   std::string module_macros;
224 
225   Target *target = exe_ctx.GetTargetPtr();
226   if (target) {
227     if (target->GetArchitecture().GetMachine() == llvm::Triple::aarch64) {
228       target_specific_defines = "typedef bool BOOL;\n";
229     }
230     if (target->GetArchitecture().GetMachine() == llvm::Triple::x86_64) {
231       if (lldb::PlatformSP platform_sp = target->GetPlatform()) {
232         static ConstString g_platform_ios_simulator("ios-simulator");
233         if (platform_sp->GetPluginName() == g_platform_ios_simulator) {
234           target_specific_defines = "typedef bool BOOL;\n";
235         }
236       }
237     }
238 
239     if (ClangModulesDeclVendor *decl_vendor =
240             target->GetClangModulesDeclVendor()) {
241       ClangPersistentVariables *persistent_vars =
242           llvm::cast<ClangPersistentVariables>(
243               target->GetPersistentExpressionStateForLanguage(
244                   lldb::eLanguageTypeC));
245       const ClangModulesDeclVendor::ModuleVector &hand_imported_modules =
246           persistent_vars->GetHandLoadedClangModules();
247       ClangModulesDeclVendor::ModuleVector modules_for_macros;
248 
249       for (ClangModulesDeclVendor::ModuleID module : hand_imported_modules) {
250         modules_for_macros.push_back(module);
251       }
252 
253       if (target->GetEnableAutoImportClangModules()) {
254         if (StackFrame *frame = exe_ctx.GetFramePtr()) {
255           if (Block *block = frame->GetFrameBlock()) {
256             SymbolContext sc;
257 
258             block->CalculateSymbolContext(&sc);
259 
260             if (sc.comp_unit) {
261               StreamString error_stream;
262 
263               decl_vendor->AddModulesForCompileUnit(
264                   *sc.comp_unit, modules_for_macros, error_stream);
265             }
266           }
267         }
268       }
269 
270       decl_vendor->ForEachMacro(
271           modules_for_macros,
272           [&module_macros](const std::string &expansion) -> bool {
273             module_macros.append(expansion);
274             module_macros.append("\n");
275             return false;
276           });
277     }
278   }
279 
280   StreamString debug_macros_stream;
281   StreamString lldb_local_var_decls;
282   if (StackFrame *frame = exe_ctx.GetFramePtr()) {
283     const SymbolContext &sc = frame->GetSymbolContext(
284         lldb::eSymbolContextCompUnit | lldb::eSymbolContextLineEntry);
285 
286     if (sc.comp_unit && sc.line_entry.IsValid()) {
287       DebugMacros *dm = sc.comp_unit->GetDebugMacros();
288       if (dm) {
289         AddMacroState state(sc.line_entry.file, sc.line_entry.line);
290         AddMacros(dm, sc.comp_unit, state, debug_macros_stream);
291       }
292     }
293 
294     if (add_locals) {
295       if (Language::LanguageIsCPlusPlus(frame->GetLanguage())) {
296         if (target->GetInjectLocalVariables(&exe_ctx)) {
297           lldb::VariableListSP var_list_sp =
298               frame->GetInScopeVariableList(false, true);
299           AddLocalVariableDecls(var_list_sp, lldb_local_var_decls,
300                                 force_add_all_locals ? "" : m_body);
301         }
302       }
303     }
304   }
305 
306   if (m_wrap) {
307     switch (wrapping_language) {
308     default:
309       return false;
310     case lldb::eLanguageTypeC:
311     case lldb::eLanguageTypeC_plus_plus:
312     case lldb::eLanguageTypeObjC:
313       break;
314     }
315 
316     // Generate a list of @import statements that will import the specified
317     // module into our expression.
318     std::string module_imports;
319     for (const std::string &module : modules) {
320       module_imports.append("@import ");
321       module_imports.append(module);
322       module_imports.append(";\n");
323     }
324 
325     StreamString wrap_stream;
326 
327     wrap_stream.Printf("%s\n%s\n%s\n%s\n%s\n", module_macros.c_str(),
328                        debug_macros_stream.GetData(), g_expression_prefix,
329                        target_specific_defines, m_prefix.c_str());
330 
331     // First construct a tagged form of the user expression so we can find it
332     // later:
333     std::string tagged_body;
334     switch (wrapping_language) {
335     default:
336       tagged_body = m_body;
337       break;
338     case lldb::eLanguageTypeC:
339     case lldb::eLanguageTypeC_plus_plus:
340     case lldb::eLanguageTypeObjC:
341       tagged_body.append(c_start_marker);
342       tagged_body.append(m_body);
343       tagged_body.append(c_end_marker);
344       break;
345     }
346     switch (wrapping_language) {
347     default:
348       break;
349     case lldb::eLanguageTypeC:
350       wrap_stream.Printf("%s"
351                          "void                           \n"
352                          "%s(void *$__lldb_arg)          \n"
353                          "{                              \n"
354                          "    %s;                        \n"
355                          "%s"
356                          "}                              \n",
357                          module_imports.c_str(), m_name.c_str(),
358                          lldb_local_var_decls.GetData(), tagged_body.c_str());
359       break;
360     case lldb::eLanguageTypeC_plus_plus:
361       wrap_stream.Printf("%s"
362                          "void                                   \n"
363                          "$__lldb_class::%s(void *$__lldb_arg)   \n"
364                          "{                                      \n"
365                          "    %s;                                \n"
366                          "%s"
367                          "}                                      \n",
368                          module_imports.c_str(), m_name.c_str(),
369                          lldb_local_var_decls.GetData(), tagged_body.c_str());
370       break;
371     case lldb::eLanguageTypeObjC:
372       if (static_method) {
373         wrap_stream.Printf(
374             "%s"
375             "@interface $__lldb_objc_class ($__lldb_category)        \n"
376             "+(void)%s:(void *)$__lldb_arg;                          \n"
377             "@end                                                    \n"
378             "@implementation $__lldb_objc_class ($__lldb_category)   \n"
379             "+(void)%s:(void *)$__lldb_arg                           \n"
380             "{                                                       \n"
381             "%s"
382             "}                                                       \n"
383             "@end                                                    \n",
384             module_imports.c_str(), m_name.c_str(), m_name.c_str(),
385             tagged_body.c_str());
386       } else {
387         wrap_stream.Printf(
388             "%s"
389             "@interface $__lldb_objc_class ($__lldb_category)       \n"
390             "-(void)%s:(void *)$__lldb_arg;                         \n"
391             "@end                                                   \n"
392             "@implementation $__lldb_objc_class ($__lldb_category)  \n"
393             "-(void)%s:(void *)$__lldb_arg                          \n"
394             "{                                                      \n"
395             "%s"
396             "}                                                      \n"
397             "@end                                                   \n",
398             module_imports.c_str(), m_name.c_str(), m_name.c_str(),
399             tagged_body.c_str());
400       }
401       break;
402     }
403 
404     text = wrap_stream.GetString();
405   } else {
406     text.append(m_body);
407   }
408 
409   return true;
410 }
411 
412 bool ClangExpressionSourceCode::GetOriginalBodyBounds(
413     std::string transformed_text, lldb::LanguageType wrapping_language,
414     size_t &start_loc, size_t &end_loc) {
415   const char *start_marker;
416   const char *end_marker;
417 
418   switch (wrapping_language) {
419   default:
420     return false;
421   case lldb::eLanguageTypeC:
422   case lldb::eLanguageTypeC_plus_plus:
423   case lldb::eLanguageTypeObjC:
424     start_marker = c_start_marker;
425     end_marker = c_end_marker;
426     break;
427   }
428 
429   start_loc = transformed_text.find(start_marker);
430   if (start_loc == std::string::npos)
431     return false;
432   start_loc += strlen(start_marker);
433   end_loc = transformed_text.find(end_marker);
434   return end_loc != std::string::npos;
435 }
436