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