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