• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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:
AddMacroState(const FileSpec & current_file,const uint32_t current_file_line)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, WrapKind wrap_kind)
178     : ExpressionSourceCode(name, prefix, body, wrap), m_wrap_kind(wrap_kind) {
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.
hasToken(llvm::StringRef token) const196   bool hasToken(llvm::StringRef token) const {
197     return m_tokens.find(token) != m_tokens.end();
198   }
199 };
200 } // namespace
201 
TokenVerifier(std::string body)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(buf->getMemBufferRef());
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->getMemBufferRef(), 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 
AddLocalVariableDecls(const lldb::VariableListSP & var_list_sp,StreamString & stream,const std::string & expr) const264 void ClangExpressionSourceCode::AddLocalVariableDecls(
265     const lldb::VariableListSP &var_list_sp, StreamString &stream,
266     const std::string &expr) const {
267   TokenVerifier tokens(expr);
268 
269   for (size_t i = 0; i < var_list_sp->GetSize(); i++) {
270     lldb::VariableSP var_sp = var_list_sp->GetVariableAtIndex(i);
271 
272     ConstString var_name = var_sp->GetName();
273 
274 
275     // We can check for .block_descriptor w/o checking for langauge since this
276     // is not a valid identifier in either C or C++.
277     if (!var_name || var_name == ".block_descriptor")
278       continue;
279 
280     if (!expr.empty() && !tokens.hasToken(var_name.GetStringRef()))
281       continue;
282 
283     const bool is_objc = m_wrap_kind == WrapKind::ObjCInstanceMethod ||
284                          m_wrap_kind == WrapKind::ObjCStaticMethod;
285     if ((var_name == "self" || var_name == "_cmd") && is_objc)
286       continue;
287 
288     if (var_name == "this" && m_wrap_kind == WrapKind::CppMemberFunction)
289       continue;
290 
291     stream.Printf("using $__lldb_local_vars::%s;\n", var_name.AsCString());
292   }
293 }
294 
GetText(std::string & text,ExecutionContext & exe_ctx,bool add_locals,bool force_add_all_locals,llvm::ArrayRef<std::string> modules) const295 bool ClangExpressionSourceCode::GetText(
296     std::string &text, ExecutionContext &exe_ctx, bool add_locals,
297     bool force_add_all_locals, llvm::ArrayRef<std::string> modules) const {
298   const char *target_specific_defines = "typedef signed char BOOL;\n";
299   std::string module_macros;
300   llvm::raw_string_ostream module_macros_stream(module_macros);
301 
302   Target *target = exe_ctx.GetTargetPtr();
303   if (target) {
304     if (target->GetArchitecture().GetMachine() == llvm::Triple::aarch64 ||
305         target->GetArchitecture().GetMachine() == llvm::Triple::aarch64_32) {
306       target_specific_defines = "typedef bool BOOL;\n";
307     }
308     if (target->GetArchitecture().GetMachine() == llvm::Triple::x86_64) {
309       if (lldb::PlatformSP platform_sp = target->GetPlatform()) {
310         static ConstString g_platform_ios_simulator("ios-simulator");
311         if (platform_sp->GetPluginName() == g_platform_ios_simulator) {
312           target_specific_defines = "typedef bool BOOL;\n";
313         }
314       }
315     }
316 
317     ClangModulesDeclVendor *decl_vendor = target->GetClangModulesDeclVendor();
318     auto *persistent_vars = llvm::cast<ClangPersistentVariables>(
319         target->GetPersistentExpressionStateForLanguage(lldb::eLanguageTypeC));
320     if (decl_vendor && persistent_vars) {
321       const ClangModulesDeclVendor::ModuleVector &hand_imported_modules =
322           persistent_vars->GetHandLoadedClangModules();
323       ClangModulesDeclVendor::ModuleVector modules_for_macros;
324 
325       for (ClangModulesDeclVendor::ModuleID module : hand_imported_modules) {
326         modules_for_macros.push_back(module);
327       }
328 
329       if (target->GetEnableAutoImportClangModules()) {
330         if (StackFrame *frame = exe_ctx.GetFramePtr()) {
331           if (Block *block = frame->GetFrameBlock()) {
332             SymbolContext sc;
333 
334             block->CalculateSymbolContext(&sc);
335 
336             if (sc.comp_unit) {
337               StreamString error_stream;
338 
339               decl_vendor->AddModulesForCompileUnit(
340                   *sc.comp_unit, modules_for_macros, error_stream);
341             }
342           }
343         }
344       }
345 
346       decl_vendor->ForEachMacro(
347           modules_for_macros,
348           [&module_macros_stream](llvm::StringRef token,
349                                   llvm::StringRef expansion) -> bool {
350             // Check if the macro hasn't already been defined in the
351             // g_expression_prefix (which defines a few builtin macros).
352             module_macros_stream << "#ifndef " << token << "\n";
353             module_macros_stream << expansion << "\n";
354             module_macros_stream << "#endif\n";
355             return false;
356           });
357     }
358   }
359 
360   StreamString debug_macros_stream;
361   StreamString lldb_local_var_decls;
362   if (StackFrame *frame = exe_ctx.GetFramePtr()) {
363     const SymbolContext &sc = frame->GetSymbolContext(
364         lldb::eSymbolContextCompUnit | lldb::eSymbolContextLineEntry);
365 
366     if (sc.comp_unit && sc.line_entry.IsValid()) {
367       DebugMacros *dm = sc.comp_unit->GetDebugMacros();
368       if (dm) {
369         AddMacroState state(sc.line_entry.file, sc.line_entry.line);
370         AddMacros(dm, sc.comp_unit, state, debug_macros_stream);
371       }
372     }
373 
374     if (add_locals)
375       if (target->GetInjectLocalVariables(&exe_ctx)) {
376         lldb::VariableListSP var_list_sp =
377             frame->GetInScopeVariableList(false, true);
378         AddLocalVariableDecls(var_list_sp, lldb_local_var_decls,
379                               force_add_all_locals ? "" : m_body);
380       }
381   }
382 
383   if (m_wrap) {
384     // Generate a list of @import statements that will import the specified
385     // module into our expression.
386     std::string module_imports;
387     for (const std::string &module : modules) {
388       module_imports.append("@import ");
389       module_imports.append(module);
390       module_imports.append(";\n");
391     }
392 
393     StreamString wrap_stream;
394 
395     wrap_stream.Printf("%s\n%s\n%s\n%s\n%s\n", g_expression_prefix,
396                        module_macros.c_str(), debug_macros_stream.GetData(),
397                        target_specific_defines, m_prefix.c_str());
398 
399     // First construct a tagged form of the user expression so we can find it
400     // later:
401     std::string tagged_body;
402     tagged_body.append(m_start_marker);
403     tagged_body.append(m_body);
404     tagged_body.append(m_end_marker);
405 
406     switch (m_wrap_kind) {
407     case WrapKind::Function:
408       wrap_stream.Printf("%s"
409                          "void                           \n"
410                          "%s(void *$__lldb_arg)          \n"
411                          "{                              \n"
412                          "    %s;                        \n"
413                          "%s"
414                          "}                              \n",
415                          module_imports.c_str(), m_name.c_str(),
416                          lldb_local_var_decls.GetData(), tagged_body.c_str());
417       break;
418     case WrapKind::CppMemberFunction:
419       wrap_stream.Printf("%s"
420                          "void                                   \n"
421                          "$__lldb_class::%s(void *$__lldb_arg)   \n"
422                          "{                                      \n"
423                          "    %s;                                \n"
424                          "%s"
425                          "}                                      \n",
426                          module_imports.c_str(), m_name.c_str(),
427                          lldb_local_var_decls.GetData(), tagged_body.c_str());
428       break;
429     case WrapKind::ObjCInstanceMethod:
430       wrap_stream.Printf(
431           "%s"
432           "@interface $__lldb_objc_class ($__lldb_category)       \n"
433           "-(void)%s:(void *)$__lldb_arg;                         \n"
434           "@end                                                   \n"
435           "@implementation $__lldb_objc_class ($__lldb_category)  \n"
436           "-(void)%s:(void *)$__lldb_arg                          \n"
437           "{                                                      \n"
438           "    %s;                                                \n"
439           "%s"
440           "}                                                      \n"
441           "@end                                                   \n",
442           module_imports.c_str(), m_name.c_str(), m_name.c_str(),
443           lldb_local_var_decls.GetData(), tagged_body.c_str());
444       break;
445 
446     case WrapKind::ObjCStaticMethod:
447       wrap_stream.Printf(
448           "%s"
449           "@interface $__lldb_objc_class ($__lldb_category)        \n"
450           "+(void)%s:(void *)$__lldb_arg;                          \n"
451           "@end                                                    \n"
452           "@implementation $__lldb_objc_class ($__lldb_category)   \n"
453           "+(void)%s:(void *)$__lldb_arg                           \n"
454           "{                                                       \n"
455           "    %s;                                                 \n"
456           "%s"
457           "}                                                       \n"
458           "@end                                                    \n",
459           module_imports.c_str(), m_name.c_str(), m_name.c_str(),
460           lldb_local_var_decls.GetData(), tagged_body.c_str());
461       break;
462     }
463 
464     text = std::string(wrap_stream.GetString());
465   } else {
466     text.append(m_body);
467   }
468 
469   return true;
470 }
471 
GetOriginalBodyBounds(std::string transformed_text,size_t & start_loc,size_t & end_loc)472 bool ClangExpressionSourceCode::GetOriginalBodyBounds(
473     std::string transformed_text, size_t &start_loc, size_t &end_loc) {
474   start_loc = transformed_text.find(m_start_marker);
475   if (start_loc == std::string::npos)
476     return false;
477   start_loc += m_start_marker.size();
478   end_loc = transformed_text.find(m_end_marker);
479   return end_loc != std::string::npos;
480 }
481