1 //===-- ClangExpressionSourceCode.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 "ClangExpressionSourceCode.h"
10 
11 #include "clang/Basic/CharInfo.h"
12 #include "clang/Basic/FileManager.h"
13 #include "clang/Basic/SourceManager.h"
14 #include "clang/Lex/Lexer.h"
15 #include "llvm/ADT/StringRef.h"
16 
17 #include "Plugins/ExpressionParser/Clang/ClangModulesDeclVendor.h"
18 #include "Plugins/ExpressionParser/Clang/ClangPersistentVariables.h"
19 #include "lldb/Symbol/Block.h"
20 #include "lldb/Symbol/CompileUnit.h"
21 #include "lldb/Symbol/DebugMacros.h"
22 #include "lldb/Symbol/TypeSystem.h"
23 #include "lldb/Symbol/VariableList.h"
24 #include "lldb/Target/ExecutionContext.h"
25 #include "lldb/Target/Language.h"
26 #include "lldb/Target/Platform.h"
27 #include "lldb/Target/StackFrame.h"
28 #include "lldb/Target/Target.h"
29 #include "lldb/Utility/StreamString.h"
30 
31 using namespace lldb_private;
32 
33 #define PREFIX_NAME "<lldb wrapper prefix>"
34 
35 const llvm::StringRef ClangExpressionSourceCode::g_prefix_file_name = PREFIX_NAME;
36 
37 const char *ClangExpressionSourceCode::g_expression_prefix =
38 "#line 1 \"" PREFIX_NAME R"("
39 #ifndef offsetof
40 #define offsetof(t, d) __builtin_offsetof(t, d)
41 #endif
42 #ifndef NULL
43 #define NULL (__null)
44 #endif
45 #ifndef Nil
46 #define Nil (__null)
47 #endif
48 #ifndef nil
49 #define nil (__null)
50 #endif
51 #ifndef YES
52 #define YES ((BOOL)1)
53 #endif
54 #ifndef NO
55 #define NO ((BOOL)0)
56 #endif
57 typedef __INT8_TYPE__ int8_t;
58 typedef __UINT8_TYPE__ uint8_t;
59 typedef __INT16_TYPE__ int16_t;
60 typedef __UINT16_TYPE__ uint16_t;
61 typedef __INT32_TYPE__ int32_t;
62 typedef __UINT32_TYPE__ uint32_t;
63 typedef __INT64_TYPE__ int64_t;
64 typedef __UINT64_TYPE__ uint64_t;
65 typedef __INTPTR_TYPE__ intptr_t;
66 typedef __UINTPTR_TYPE__ uintptr_t;
67 typedef __SIZE_TYPE__ size_t;
68 typedef __PTRDIFF_TYPE__ ptrdiff_t;
69 typedef unsigned short unichar;
70 extern "C"
71 {
72     int printf(const char * __restrict, ...);
73 }
74 )";
75 
76 namespace {
77 
78 class AddMacroState {
79   enum State {
80     CURRENT_FILE_NOT_YET_PUSHED,
81     CURRENT_FILE_PUSHED,
82     CURRENT_FILE_POPPED
83   };
84 
85 public:
86   AddMacroState(const FileSpec &current_file, const uint32_t current_file_line)
87       : m_state(CURRENT_FILE_NOT_YET_PUSHED), m_current_file(current_file),
88         m_current_file_line(current_file_line) {}
89 
90   void StartFile(const FileSpec &file) {
91     m_file_stack.push_back(file);
92     if (file == m_current_file)
93       m_state = CURRENT_FILE_PUSHED;
94   }
95 
96   void EndFile() {
97     if (m_file_stack.size() == 0)
98       return;
99 
100     FileSpec old_top = m_file_stack.back();
101     m_file_stack.pop_back();
102     if (old_top == m_current_file)
103       m_state = CURRENT_FILE_POPPED;
104   }
105 
106   // An entry is valid if it occurs before the current line in the current
107   // file.
108   bool IsValidEntry(uint32_t line) {
109     switch (m_state) {
110     case CURRENT_FILE_NOT_YET_PUSHED:
111       return true;
112     case CURRENT_FILE_PUSHED:
113       // If we are in file included in the current file, the entry should be
114       // added.
115       if (m_file_stack.back() != m_current_file)
116         return true;
117 
118       return line < m_current_file_line;
119     default:
120       return false;
121     }
122   }
123 
124 private:
125   std::vector<FileSpec> m_file_stack;
126   State m_state;
127   FileSpec m_current_file;
128   uint32_t m_current_file_line;
129 };
130 
131 } // anonymous namespace
132 
133 static void AddMacros(const DebugMacros *dm, CompileUnit *comp_unit,
134                       AddMacroState &state, StreamString &stream) {
135   if (dm == nullptr)
136     return;
137 
138   for (size_t i = 0; i < dm->GetNumMacroEntries(); i++) {
139     const DebugMacroEntry &entry = dm->GetMacroEntryAtIndex(i);
140     uint32_t line;
141 
142     switch (entry.GetType()) {
143     case DebugMacroEntry::DEFINE:
144       if (state.IsValidEntry(entry.GetLineNumber()))
145         stream.Printf("#define %s\n", entry.GetMacroString().AsCString());
146       else
147         return;
148       break;
149     case DebugMacroEntry::UNDEF:
150       if (state.IsValidEntry(entry.GetLineNumber()))
151         stream.Printf("#undef %s\n", entry.GetMacroString().AsCString());
152       else
153         return;
154       break;
155     case DebugMacroEntry::START_FILE:
156       line = entry.GetLineNumber();
157       if (state.IsValidEntry(line))
158         state.StartFile(entry.GetFileSpec(comp_unit));
159       else
160         return;
161       break;
162     case DebugMacroEntry::END_FILE:
163       state.EndFile();
164       break;
165     case DebugMacroEntry::INDIRECT:
166       AddMacros(entry.GetIndirectDebugMacros(), comp_unit, state, stream);
167       break;
168     default:
169       // This is an unknown/invalid entry. Ignore.
170       break;
171     }
172   }
173 }
174 
175 lldb_private::ClangExpressionSourceCode::ClangExpressionSourceCode(
176     llvm::StringRef filename, llvm::StringRef name, llvm::StringRef prefix,
177     llvm::StringRef body, Wrapping wrap)
178     : ExpressionSourceCode(name, prefix, body, wrap) {
179   // Use #line markers to pretend that we have a single-line source file
180   // containing only the user expression. This will hide our wrapper code
181   // from the user when we render diagnostics with Clang.
182   m_start_marker = "#line 1 \"" + filename.str() + "\"\n";
183   m_end_marker = "\n;\n#line 1 \"<lldb wrapper suffix>\"\n";
184 }
185 
186 namespace {
187 /// Allows checking if a token is contained in a given expression.
188 class TokenVerifier {
189   /// The tokens we found in the expression.
190   llvm::StringSet<> m_tokens;
191 
192 public:
193   TokenVerifier(std::string body);
194   /// Returns true iff the given expression body contained a token with the
195   /// given content.
196   bool hasToken(llvm::StringRef token) const {
197     return m_tokens.find(token) != m_tokens.end();
198   }
199 };
200 } // namespace
201 
202 TokenVerifier::TokenVerifier(std::string body) {
203   using namespace clang;
204 
205   // We only care about tokens and not their original source locations. If we
206   // move the whole expression to only be in one line we can simplify the
207   // following code that extracts the token contents.
208   std::replace(body.begin(), body.end(), '\n', ' ');
209   std::replace(body.begin(), body.end(), '\r', ' ');
210 
211   FileSystemOptions file_opts;
212   FileManager file_mgr(file_opts,
213                        FileSystem::Instance().GetVirtualFileSystem());
214 
215   // Let's build the actual source code Clang needs and setup some utility
216   // objects.
217   llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_ids(new DiagnosticIDs());
218   llvm::IntrusiveRefCntPtr<DiagnosticOptions> diags_opts(
219       new DiagnosticOptions());
220   DiagnosticsEngine diags(diag_ids, diags_opts);
221   clang::SourceManager SM(diags, file_mgr);
222   auto buf = llvm::MemoryBuffer::getMemBuffer(body);
223 
224   FileID FID = SM.createFileID(clang::SourceManager::Unowned, buf.get());
225 
226   // Let's just enable the latest ObjC and C++ which should get most tokens
227   // right.
228   LangOptions Opts;
229   Opts.ObjC = true;
230   Opts.DollarIdents = true;
231   Opts.CPlusPlus17 = true;
232   Opts.LineComment = true;
233 
234   Lexer lex(FID, buf.get(), SM, Opts);
235 
236   Token token;
237   bool exit = false;
238   while (!exit) {
239     // Returns true if this is the last token we get from the lexer.
240     exit = lex.LexFromRawLexer(token);
241 
242     // Extract the column number which we need to extract the token content.
243     // Our expression is just one line, so we don't need to handle any line
244     // numbers here.
245     bool invalid = false;
246     unsigned start = SM.getSpellingColumnNumber(token.getLocation(), &invalid);
247     if (invalid)
248       continue;
249     // Column numbers start at 1, but indexes in our string start at 0.
250     --start;
251 
252     // Annotations don't have a length, so let's skip them.
253     if (token.isAnnotation())
254       continue;
255 
256     // Extract the token string from our source code and store it.
257     std::string token_str = body.substr(start, token.getLength());
258     if (token_str.empty())
259       continue;
260     m_tokens.insert(token_str);
261   }
262 }
263 
264 static void AddLocalVariableDecls(const lldb::VariableListSP &var_list_sp,
265                                   StreamString &stream,
266                                   const std::string &expr,
267                                   lldb::LanguageType wrapping_language) {
268   TokenVerifier tokens(expr);
269 
270   for (size_t i = 0; i < var_list_sp->GetSize(); i++) {
271     lldb::VariableSP var_sp = var_list_sp->GetVariableAtIndex(i);
272 
273     ConstString var_name = var_sp->GetName();
274 
275 
276     // We can check for .block_descriptor w/o checking for langauge since this
277     // is not a valid identifier in either C or C++.
278     if (!var_name || var_name == ".block_descriptor")
279       continue;
280 
281     if (!expr.empty() && !tokens.hasToken(var_name.GetStringRef()))
282       continue;
283 
284     if ((var_name == "self" || var_name == "_cmd") &&
285         (wrapping_language == lldb::eLanguageTypeObjC ||
286          wrapping_language == lldb::eLanguageTypeObjC_plus_plus))
287       continue;
288 
289     if (var_name == "this" &&
290         wrapping_language == lldb::eLanguageTypeC_plus_plus)
291       continue;
292 
293     stream.Printf("using $__lldb_local_vars::%s;\n", var_name.AsCString());
294   }
295 }
296 
297 bool ClangExpressionSourceCode::GetText(
298     std::string &text, lldb::LanguageType wrapping_language, bool static_method,
299     ExecutionContext &exe_ctx, bool add_locals, bool force_add_all_locals,
300     llvm::ArrayRef<std::string> modules) const {
301   const char *target_specific_defines = "typedef signed char BOOL;\n";
302   std::string module_macros;
303 
304   Target *target = exe_ctx.GetTargetPtr();
305   if (target) {
306     if (target->GetArchitecture().GetMachine() == llvm::Triple::aarch64 ||
307         target->GetArchitecture().GetMachine() == llvm::Triple::aarch64_32) {
308       target_specific_defines = "typedef bool BOOL;\n";
309     }
310     if (target->GetArchitecture().GetMachine() == llvm::Triple::x86_64) {
311       if (lldb::PlatformSP platform_sp = target->GetPlatform()) {
312         static ConstString g_platform_ios_simulator("ios-simulator");
313         if (platform_sp->GetPluginName() == g_platform_ios_simulator) {
314           target_specific_defines = "typedef bool BOOL;\n";
315         }
316       }
317     }
318 
319     ClangModulesDeclVendor *decl_vendor = target->GetClangModulesDeclVendor();
320     auto *persistent_vars = llvm::cast<ClangPersistentVariables>(
321         target->GetPersistentExpressionStateForLanguage(lldb::eLanguageTypeC));
322     if (decl_vendor && persistent_vars) {
323       const ClangModulesDeclVendor::ModuleVector &hand_imported_modules =
324           persistent_vars->GetHandLoadedClangModules();
325       ClangModulesDeclVendor::ModuleVector modules_for_macros;
326 
327       for (ClangModulesDeclVendor::ModuleID module : hand_imported_modules) {
328         modules_for_macros.push_back(module);
329       }
330 
331       if (target->GetEnableAutoImportClangModules()) {
332         if (StackFrame *frame = exe_ctx.GetFramePtr()) {
333           if (Block *block = frame->GetFrameBlock()) {
334             SymbolContext sc;
335 
336             block->CalculateSymbolContext(&sc);
337 
338             if (sc.comp_unit) {
339               StreamString error_stream;
340 
341               decl_vendor->AddModulesForCompileUnit(
342                   *sc.comp_unit, modules_for_macros, error_stream);
343             }
344           }
345         }
346       }
347 
348       decl_vendor->ForEachMacro(
349           modules_for_macros,
350           [&module_macros](const std::string &expansion) -> bool {
351             module_macros.append(expansion);
352             module_macros.append("\n");
353             return false;
354           });
355     }
356   }
357 
358   StreamString debug_macros_stream;
359   StreamString lldb_local_var_decls;
360   if (StackFrame *frame = exe_ctx.GetFramePtr()) {
361     const SymbolContext &sc = frame->GetSymbolContext(
362         lldb::eSymbolContextCompUnit | lldb::eSymbolContextLineEntry);
363 
364     if (sc.comp_unit && sc.line_entry.IsValid()) {
365       DebugMacros *dm = sc.comp_unit->GetDebugMacros();
366       if (dm) {
367         AddMacroState state(sc.line_entry.file, sc.line_entry.line);
368         AddMacros(dm, sc.comp_unit, state, debug_macros_stream);
369       }
370     }
371 
372     if (add_locals)
373       if (target->GetInjectLocalVariables(&exe_ctx)) {
374         lldb::VariableListSP var_list_sp =
375             frame->GetInScopeVariableList(false, true);
376         AddLocalVariableDecls(var_list_sp, lldb_local_var_decls,
377                               force_add_all_locals ? "" : m_body,
378                               wrapping_language);
379       }
380   }
381 
382   if (m_wrap) {
383     switch (wrapping_language) {
384     default:
385       return false;
386     case lldb::eLanguageTypeC:
387     case lldb::eLanguageTypeC_plus_plus:
388     case lldb::eLanguageTypeObjC:
389       break;
390     }
391 
392     // Generate a list of @import statements that will import the specified
393     // module into our expression.
394     std::string module_imports;
395     for (const std::string &module : modules) {
396       module_imports.append("@import ");
397       module_imports.append(module);
398       module_imports.append(";\n");
399     }
400 
401     StreamString wrap_stream;
402 
403     wrap_stream.Printf("%s\n%s\n%s\n%s\n%s\n", module_macros.c_str(),
404                        debug_macros_stream.GetData(), g_expression_prefix,
405                        target_specific_defines, m_prefix.c_str());
406 
407     // First construct a tagged form of the user expression so we can find it
408     // later:
409     std::string tagged_body;
410     switch (wrapping_language) {
411     default:
412       tagged_body = m_body;
413       break;
414     case lldb::eLanguageTypeC:
415     case lldb::eLanguageTypeC_plus_plus:
416     case lldb::eLanguageTypeObjC:
417       tagged_body.append(m_start_marker);
418       tagged_body.append(m_body);
419       tagged_body.append(m_end_marker);
420       break;
421     }
422     switch (wrapping_language) {
423     default:
424       break;
425     case lldb::eLanguageTypeC:
426       wrap_stream.Printf("%s"
427                          "void                           \n"
428                          "%s(void *$__lldb_arg)          \n"
429                          "{                              \n"
430                          "    %s;                        \n"
431                          "%s"
432                          "}                              \n",
433                          module_imports.c_str(), m_name.c_str(),
434                          lldb_local_var_decls.GetData(), tagged_body.c_str());
435       break;
436     case lldb::eLanguageTypeC_plus_plus:
437       wrap_stream.Printf("%s"
438                          "void                                   \n"
439                          "$__lldb_class::%s(void *$__lldb_arg)   \n"
440                          "{                                      \n"
441                          "    %s;                                \n"
442                          "%s"
443                          "}                                      \n",
444                          module_imports.c_str(), m_name.c_str(),
445                          lldb_local_var_decls.GetData(), tagged_body.c_str());
446       break;
447     case lldb::eLanguageTypeObjC:
448       if (static_method) {
449         wrap_stream.Printf(
450             "%s"
451             "@interface $__lldb_objc_class ($__lldb_category)        \n"
452             "+(void)%s:(void *)$__lldb_arg;                          \n"
453             "@end                                                    \n"
454             "@implementation $__lldb_objc_class ($__lldb_category)   \n"
455             "+(void)%s:(void *)$__lldb_arg                           \n"
456             "{                                                       \n"
457             "    %s;                                                 \n"
458             "%s"
459             "}                                                       \n"
460             "@end                                                    \n",
461             module_imports.c_str(), m_name.c_str(), m_name.c_str(),
462             lldb_local_var_decls.GetData(), tagged_body.c_str());
463       } else {
464         wrap_stream.Printf(
465             "%s"
466             "@interface $__lldb_objc_class ($__lldb_category)       \n"
467             "-(void)%s:(void *)$__lldb_arg;                         \n"
468             "@end                                                   \n"
469             "@implementation $__lldb_objc_class ($__lldb_category)  \n"
470             "-(void)%s:(void *)$__lldb_arg                          \n"
471             "{                                                      \n"
472             "    %s;                                                \n"
473             "%s"
474             "}                                                      \n"
475             "@end                                                   \n",
476             module_imports.c_str(), m_name.c_str(), m_name.c_str(),
477             lldb_local_var_decls.GetData(), tagged_body.c_str());
478       }
479       break;
480     }
481 
482     text = std::string(wrap_stream.GetString());
483   } else {
484     text.append(m_body);
485   }
486 
487   return true;
488 }
489 
490 bool ClangExpressionSourceCode::GetOriginalBodyBounds(
491     std::string transformed_text, lldb::LanguageType wrapping_language,
492     size_t &start_loc, size_t &end_loc) {
493   switch (wrapping_language) {
494   default:
495     return false;
496   case lldb::eLanguageTypeC:
497   case lldb::eLanguageTypeC_plus_plus:
498   case lldb::eLanguageTypeObjC:
499     break;
500   }
501 
502   start_loc = transformed_text.find(m_start_marker);
503   if (start_loc == std::string::npos)
504     return false;
505   start_loc += m_start_marker.size();
506   end_loc = transformed_text.find(m_end_marker);
507   return end_loc != std::string::npos;
508 }
509