1 //===-- ClangExpressionDeclMap.h --------------------------------*- 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 #ifndef liblldb_ClangExpressionDeclMap_h_ 10 #define liblldb_ClangExpressionDeclMap_h_ 11 12 #include <signal.h> 13 #include <stdint.h> 14 15 #include <vector> 16 17 #include "ClangASTSource.h" 18 #include "ClangExpressionVariable.h" 19 20 #include "lldb/Core/Value.h" 21 #include "lldb/Expression/Materializer.h" 22 #include "lldb/Symbol/SymbolContext.h" 23 #include "lldb/Symbol/TaggedASTType.h" 24 #include "lldb/Target/ExecutionContext.h" 25 #include "lldb/lldb-public.h" 26 #include "clang/AST/Decl.h" 27 #include "llvm/ADT/DenseMap.h" 28 29 namespace lldb_private { 30 31 /// \class ClangExpressionDeclMap ClangExpressionDeclMap.h 32 /// "lldb/Expression/ClangExpressionDeclMap.h" Manages named entities that are 33 /// defined in LLDB's debug information. 34 /// 35 /// The Clang parser uses the ClangASTSource as an interface to request named 36 /// entities from outside an expression. The ClangASTSource reports back, 37 /// listing all possible objects corresponding to a particular name. But it 38 /// in turn relies on ClangExpressionDeclMap, which performs several important 39 /// functions. 40 /// 41 /// First, it records what variables and functions were looked up and what 42 /// Decls were returned for them. 43 /// 44 /// Second, it constructs a struct on behalf of IRForTarget, recording which 45 /// variables should be placed where and relaying this information back so 46 /// that IRForTarget can generate context-independent code. 47 /// 48 /// Third, it "materializes" this struct on behalf of the expression command, 49 /// finding the current values of each variable and placing them into the 50 /// struct so that it can be passed to the JITted version of the IR. 51 /// 52 /// Fourth and finally, it "dematerializes" the struct after the JITted code 53 /// has has executed, placing the new values back where it found the old ones. 54 class ClangExpressionDeclMap : public ClangASTSource { 55 public: 56 /// Constructor 57 /// 58 /// Initializes class variables. 59 /// 60 /// \param[in] keep_result_in_memory 61 /// If true, inhibits the normal deallocation of the memory for 62 /// the result persistent variable, and instead marks the variable 63 /// as persisting. 64 /// 65 /// \param[in] result_delegate 66 /// If non-NULL, use this delegate to report result values. This 67 /// allows the client ClangUserExpression to report a result. 68 /// 69 /// \param[in] target 70 /// The target to use when parsing. 71 /// 72 /// \param[in] importer 73 /// The ClangASTImporter to use when parsing. 74 /// 75 /// \param[in] ctx_obj 76 /// If not empty, then expression is evaluated in context of this object. 77 /// See the comment to `UserExpression::Evaluate` for details. 78 ClangExpressionDeclMap( 79 bool keep_result_in_memory, 80 Materializer::PersistentVariableDelegate *result_delegate, 81 const lldb::TargetSP &target, const lldb::ClangASTImporterSP &importer, 82 ValueObject *ctx_obj); 83 84 /// Destructor 85 ~ClangExpressionDeclMap() override; 86 87 /// Enable the state needed for parsing and IR transformation. 88 /// 89 /// \param[in] exe_ctx 90 /// The execution context to use when finding types for variables. 91 /// Also used to find a "scratch" AST context to store result types. 92 /// 93 /// \param[in] materializer 94 /// If non-NULL, the materializer to populate with information about 95 /// the variables to use 96 /// 97 /// \return 98 /// True if parsing is possible; false if it is unsafe to continue. 99 bool WillParse(ExecutionContext &exe_ctx, Materializer *materializer); 100 101 void InstallCodeGenerator(clang::ASTConsumer *code_gen); 102 103 /// Disable the state needed for parsing and IR transformation. 104 void DidParse(); 105 106 /// [Used by IRForTarget] Add a variable to the list of persistent 107 /// variables for the process. 108 /// 109 /// \param[in] decl 110 /// The Clang declaration for the persistent variable, used for 111 /// lookup during parsing. 112 /// 113 /// \param[in] name 114 /// The name of the persistent variable, usually $something. 115 /// 116 /// \param[in] type 117 /// The type of the variable, in the Clang parser's context. 118 /// 119 /// \return 120 /// True on success; false otherwise. 121 bool AddPersistentVariable(const clang::NamedDecl *decl, 122 ConstString name, TypeFromParser type, 123 bool is_result, bool is_lvalue); 124 125 /// [Used by IRForTarget] Add a variable to the struct that needs to 126 /// be materialized each time the expression runs. 127 /// 128 /// \param[in] decl 129 /// The Clang declaration for the variable. 130 /// 131 /// \param[in] name 132 /// The name of the variable. 133 /// 134 /// \param[in] value 135 /// The LLVM IR value for this variable. 136 /// 137 /// \param[in] size 138 /// The size of the variable in bytes. 139 /// 140 /// \param[in] alignment 141 /// The required alignment of the variable in bytes. 142 /// 143 /// \return 144 /// True on success; false otherwise. 145 bool AddValueToStruct(const clang::NamedDecl *decl, ConstString name, 146 llvm::Value *value, size_t size, 147 lldb::offset_t alignment); 148 149 /// [Used by IRForTarget] Finalize the struct, laying out the position of 150 /// each object in it. 151 /// 152 /// \return 153 /// True on success; false otherwise. 154 bool DoStructLayout(); 155 156 /// [Used by IRForTarget] Get general information about the laid-out struct 157 /// after DoStructLayout() has been called. 158 /// 159 /// \param[out] num_elements 160 /// The number of elements in the struct. 161 /// 162 /// \param[out] size 163 /// The size of the struct, in bytes. 164 /// 165 /// \param[out] alignment 166 /// The alignment of the struct, in bytes. 167 /// 168 /// \return 169 /// True if the information could be retrieved; false otherwise. 170 bool GetStructInfo(uint32_t &num_elements, size_t &size, 171 lldb::offset_t &alignment); 172 173 /// [Used by IRForTarget] Get specific information about one field of the 174 /// laid-out struct after DoStructLayout() has been called. 175 /// 176 /// \param[out] decl 177 /// The parsed Decl for the field, as generated by ClangASTSource 178 /// on ClangExpressionDeclMap's behalf. In the case of the result 179 /// value, this will have the name $__lldb_result even if the 180 /// result value ends up having the name $1. This is an 181 /// implementation detail of IRForTarget. 182 /// 183 /// \param[out] value 184 /// The IR value for the field (usually a GlobalVariable). In 185 /// the case of the result value, this will have the correct 186 /// name ($1, for instance). This is an implementation detail 187 /// of IRForTarget. 188 /// 189 /// \param[out] offset 190 /// The offset of the field from the beginning of the struct. 191 /// As long as the struct is aligned according to its required 192 /// alignment, this offset will align the field correctly. 193 /// 194 /// \param[out] name 195 /// The name of the field as used in materialization. 196 /// 197 /// \param[in] index 198 /// The index of the field about which information is requested. 199 /// 200 /// \return 201 /// True if the information could be retrieved; false otherwise. 202 bool GetStructElement(const clang::NamedDecl *&decl, llvm::Value *&value, 203 lldb::offset_t &offset, ConstString &name, 204 uint32_t index); 205 206 /// [Used by IRForTarget] Get information about a function given its Decl. 207 /// 208 /// \param[in] decl 209 /// The parsed Decl for the Function, as generated by ClangASTSource 210 /// on ClangExpressionDeclMap's behalf. 211 /// 212 /// \param[out] ptr 213 /// The absolute address of the function in the target. 214 /// 215 /// \return 216 /// True if the information could be retrieved; false otherwise. 217 bool GetFunctionInfo(const clang::NamedDecl *decl, uint64_t &ptr); 218 219 /// [Used by IRForTarget] Get the address of a symbol given nothing but its 220 /// name. 221 /// 222 /// \param[in] target 223 /// The target to find the symbol in. If not provided, 224 /// then the current parsing context's Target. 225 /// 226 /// \param[in] process 227 /// The process to use. For Objective-C symbols, the process's 228 /// Objective-C language runtime may be queried if the process 229 /// is non-NULL. 230 /// 231 /// \param[in] name 232 /// The name of the symbol. 233 /// 234 /// \param[in] module 235 /// The module to limit the search to. This can be NULL 236 /// 237 /// \return 238 /// Valid load address for the symbol 239 lldb::addr_t GetSymbolAddress(Target &target, Process *process, 240 ConstString name, lldb::SymbolType symbol_type, 241 Module *module = nullptr); 242 243 lldb::addr_t GetSymbolAddress(ConstString name, 244 lldb::SymbolType symbol_type); 245 246 struct TargetInfo { 247 lldb::ByteOrder byte_order; 248 size_t address_byte_size; 249 250 TargetInfo() : byte_order(lldb::eByteOrderInvalid), address_byte_size(0) {} 251 252 bool IsValid() { 253 return (byte_order != lldb::eByteOrderInvalid && address_byte_size != 0); 254 } 255 }; 256 TargetInfo GetTargetInfo(); 257 258 /// [Used by ClangASTSource] Find all entities matching a given name, using 259 /// a NameSearchContext to make Decls for them. 260 /// 261 /// \param[in] context 262 /// The NameSearchContext that can construct Decls for this name. 263 void FindExternalVisibleDecls(NameSearchContext &context) override; 264 265 /// Find all entities matching a given name in a given module/namespace, 266 /// using a NameSearchContext to make Decls for them. 267 /// 268 /// \param[in] context 269 /// The NameSearchContext that can construct Decls for this name. 270 /// 271 /// \param[in] module 272 /// If non-NULL, the module to query. 273 /// 274 /// \param[in] namespace_decl 275 /// If valid and module is non-NULL, the parent namespace. 276 /// 277 /// \param[in] current_id 278 /// The ID for the current FindExternalVisibleDecls invocation, 279 /// for logging purposes. 280 void FindExternalVisibleDecls(NameSearchContext &context, 281 lldb::ModuleSP module, 282 CompilerDeclContext &namespace_decl, 283 unsigned int current_id); 284 285 protected: 286 /// Retrieves the declaration with the given name from the storage of 287 /// persistent declarations. 288 /// 289 /// \return 290 /// A persistent decl with the given name or a nullptr. 291 virtual clang::NamedDecl *GetPersistentDecl(ConstString name); 292 293 private: 294 ExpressionVariableList 295 m_found_entities; ///< All entities that were looked up for the parser. 296 ExpressionVariableList 297 m_struct_members; ///< All entities that need to be placed in the struct. 298 bool m_keep_result_in_memory; ///< True if result persistent variables 299 ///generated by this expression should stay in 300 ///memory. 301 Materializer::PersistentVariableDelegate 302 *m_result_delegate; ///< If non-NULL, used to report expression results to 303 ///ClangUserExpression. 304 ValueObject *m_ctx_obj; ///< If not empty, then expression is 305 ///evaluated in context of this object. 306 ///For details see the comment to 307 ///`UserExpression::Evaluate`. 308 309 /// The following values should not live beyond parsing 310 class ParserVars { 311 public: 312 ParserVars() {} 313 314 Target *GetTarget() { 315 if (m_exe_ctx.GetTargetPtr()) 316 return m_exe_ctx.GetTargetPtr(); 317 else if (m_sym_ctx.target_sp) 318 return m_sym_ctx.target_sp.get(); 319 return nullptr; 320 } 321 322 ExecutionContext m_exe_ctx; ///< The execution context to use when parsing. 323 SymbolContext m_sym_ctx; ///< The symbol context to use in finding variables 324 ///and types. 325 ClangPersistentVariables *m_persistent_vars = 326 nullptr; ///< The persistent variables for the process. 327 bool m_enable_lookups = false; ///< Set to true during parsing if we have 328 ///found the first "$__lldb" name. 329 TargetInfo m_target_info; ///< Basic information about the target. 330 Materializer *m_materializer = nullptr; ///< If non-NULL, the materializer 331 ///to use when reporting used 332 ///variables. 333 clang::ASTConsumer *m_code_gen = nullptr; ///< If non-NULL, a code generator 334 ///that receives new top-level 335 ///functions. 336 private: 337 DISALLOW_COPY_AND_ASSIGN(ParserVars); 338 }; 339 340 std::unique_ptr<ParserVars> m_parser_vars; 341 342 /// Activate parser-specific variables 343 void EnableParserVars() { 344 if (!m_parser_vars.get()) 345 m_parser_vars = std::make_unique<ParserVars>(); 346 } 347 348 /// Deallocate parser-specific variables 349 void DisableParserVars() { m_parser_vars.reset(); } 350 351 /// The following values contain layout information for the materialized 352 /// struct, but are not specific to a single materialization 353 struct StructVars { 354 StructVars() 355 : m_struct_alignment(0), m_struct_size(0), m_struct_laid_out(false), 356 m_result_name(), m_object_pointer_type(nullptr, nullptr) {} 357 358 lldb::offset_t 359 m_struct_alignment; ///< The alignment of the struct in bytes. 360 size_t m_struct_size; ///< The size of the struct in bytes. 361 bool m_struct_laid_out; ///< True if the struct has been laid out and the 362 ///layout is valid (that is, no new fields have been 363 ///added since). 364 ConstString 365 m_result_name; ///< The name of the result variable ($1, for example) 366 TypeFromUser m_object_pointer_type; ///< The type of the "this" variable, if 367 ///one exists 368 }; 369 370 std::unique_ptr<StructVars> m_struct_vars; 371 372 /// Activate struct variables 373 void EnableStructVars() { 374 if (!m_struct_vars.get()) 375 m_struct_vars.reset(new struct StructVars); 376 } 377 378 /// Deallocate struct variables 379 void DisableStructVars() { m_struct_vars.reset(); } 380 381 /// Get this parser's ID for use in extracting parser- and JIT-specific data 382 /// from persistent variables. 383 uint64_t GetParserID() { return (uint64_t) this; } 384 385 /// Should be called on all copied functions. 386 void MaybeRegisterFunctionBody(clang::FunctionDecl *copied_function_decl); 387 388 /// Searches the persistent decls of the target for entities with the 389 /// given name. 390 /// 391 /// \param[in] context 392 /// The NameSearchContext that can construct Decls for this name. 393 /// 394 /// \param[in] name 395 /// The name of the entities that need to be found. 396 /// 397 /// \param[in] current_id 398 /// The ID for the current FindExternalVisibleDecls invocation, 399 /// for logging purposes. 400 void SearchPersistenDecls(NameSearchContext &context, const ConstString name, 401 unsigned int current_id); 402 403 /// Handles looking up $__lldb_class which requires special treatment. 404 /// 405 /// \param[in] context 406 /// The NameSearchContext that can construct Decls for this name. 407 /// 408 /// \param[in] current_id 409 /// The ID for the current FindExternalVisibleDecls invocation, 410 /// for logging purposes. 411 void LookUpLldbClass(NameSearchContext &context, unsigned int current_id); 412 413 /// Handles looking up $__lldb_objc_class which requires special treatment. 414 /// 415 /// \param[in] context 416 /// The NameSearchContext that can construct Decls for this name. 417 /// 418 /// \param[in] current_id 419 /// The ID for the current FindExternalVisibleDecls invocation, 420 /// for logging purposes. 421 void LookUpLldbObjCClass(NameSearchContext &context, unsigned int current_id); 422 423 /// Handles looking up the synthetic namespace that contains our local 424 /// variables for the current frame. 425 /// 426 /// \param[in] sym_ctx 427 /// The current SymbolContext of this frame. 428 /// 429 /// \param[in] name_context 430 /// The NameSearchContext that can construct Decls for this name. 431 void LookupLocalVarNamespace(SymbolContext &sym_ctx, 432 NameSearchContext &name_context); 433 434 /// Lookup entities in the ClangModulesDeclVendor. 435 /// \param[in] context 436 /// The NameSearchContext that can construct Decls for this name. 437 /// 438 /// \param[in] name 439 /// The name of the entities that need to be found. 440 /// 441 /// \param[in] current_id 442 /// The ID for the current FindExternalVisibleDecls invocation, 443 /// for logging purposes. 444 void LookupInModulesDeclVendor(NameSearchContext &context, ConstString name, 445 unsigned current_id); 446 447 /// Looks up a local variable. 448 /// 449 /// \param[in] context 450 /// The NameSearchContext that can construct Decls for this name. 451 /// 452 /// \param[in] name 453 /// The name of the entities that need to be found. 454 /// 455 /// \param[in] current_id 456 /// The ID for the current FindExternalVisibleDecls invocation, 457 /// for logging purposes. 458 /// 459 /// \param[in] sym_ctx 460 /// The current SymbolContext of this frame. 461 /// 462 /// \param[in] namespace_decl 463 /// The parent namespace if there is one. 464 /// 465 /// \return 466 /// True iff a local variable was found. 467 bool LookupLocalVariable(NameSearchContext &context, ConstString name, 468 unsigned current_id, SymbolContext &sym_ctx, 469 CompilerDeclContext &namespace_decl); 470 471 /// Searches for functions in the given SymbolContextList. 472 /// 473 /// \param[in] sc_list 474 /// The SymbolContextList to search. 475 /// 476 /// \param[in] frame_decl_context 477 /// The current DeclContext of the current frame. 478 /// 479 /// \return 480 /// A SymbolContextList with any found functions in the front and 481 /// any unknown SymbolContexts which are not functions in the back. 482 /// The SymbolContexts for the functions are ordered by how close they are 483 /// to the DeclContext for the given frame DeclContext. 484 SymbolContextList SearchFunctionsInSymbolContexts( 485 const SymbolContextList &sc_list, 486 const CompilerDeclContext &frame_decl_context); 487 488 /// Looks up a function. 489 /// 490 /// \param[in] context 491 /// The NameSearchContext that can construct Decls for this name. 492 /// 493 /// \param[in] module_sp 494 /// If non-NULL, the module to query. 495 /// 496 /// \param[in] name 497 /// The name of the function that should be find. 498 /// 499 /// \param[in] namespace_decl 500 /// If valid and module is non-NULL, the parent namespace. 501 /// 502 /// \param[in] current_id 503 /// The ID for the current FindExternalVisibleDecls invocation, 504 /// for logging purposes. 505 void LookupFunction(NameSearchContext &context, lldb::ModuleSP module_sp, 506 ConstString name, CompilerDeclContext &namespace_decl, 507 unsigned current_id); 508 509 /// Given a target, find a variable that matches the given name and type. 510 /// 511 /// \param[in] target 512 /// The target to use as a basis for finding the variable. 513 /// 514 /// \param[in] module 515 /// If non-NULL, the module to search. 516 /// 517 /// \param[in] name 518 /// The name as a plain C string. 519 /// 520 /// \param[in] namespace_decl 521 /// If non-NULL and module is non-NULL, the parent namespace. 522 /// 523 /// \return 524 /// The LLDB Variable found, or NULL if none was found. 525 lldb::VariableSP FindGlobalVariable(Target &target, lldb::ModuleSP &module, 526 ConstString name, 527 CompilerDeclContext *namespace_decl); 528 529 /// Get the value of a variable in a given execution context and return the 530 /// associated Types if needed. 531 /// 532 /// \param[in] var 533 /// The variable to evaluate. 534 /// 535 /// \param[out] var_location 536 /// The variable location value to fill in 537 /// 538 /// \param[out] found_type 539 /// The type of the found value, as it was found in the user process. 540 /// This is only useful when the variable is being inspected on behalf 541 /// of the parser, hence the default. 542 /// 543 /// \param[out] parser_type 544 /// The type of the found value, as it was copied into the parser's 545 /// AST context. This is only useful when the variable is being 546 /// inspected on behalf of the parser, hence the default. 547 /// 548 /// \return 549 /// Return true if the value was successfully filled in. 550 bool GetVariableValue(lldb::VariableSP &var, 551 lldb_private::Value &var_location, 552 TypeFromUser *found_type = nullptr, 553 TypeFromParser *parser_type = nullptr); 554 555 /// Use the NameSearchContext to generate a Decl for the given LLDB 556 /// Variable, and put it in the Tuple list. 557 /// 558 /// \param[in] context 559 /// The NameSearchContext to use when constructing the Decl. 560 /// 561 /// \param[in] var 562 /// The LLDB Variable that needs a Decl. 563 /// 564 /// \param[in] valobj 565 /// The LLDB ValueObject for that variable. 566 void AddOneVariable(NameSearchContext &context, lldb::VariableSP var, 567 lldb::ValueObjectSP valobj, unsigned int current_id); 568 569 /// Use the NameSearchContext to generate a Decl for the given persistent 570 /// variable, and put it in the list of found entities. 571 /// 572 /// \param[in] context 573 /// The NameSearchContext to use when constructing the Decl. 574 /// 575 /// \param[in] pvar_sp 576 /// The persistent variable that needs a Decl. 577 /// 578 /// \param[in] current_id 579 /// The ID of the current invocation of FindExternalVisibleDecls 580 /// for logging purposes. 581 void AddOneVariable(NameSearchContext &context, 582 lldb::ExpressionVariableSP &pvar_sp, 583 unsigned int current_id); 584 585 /// Use the NameSearchContext to generate a Decl for the given LLDB symbol 586 /// (treated as a variable), and put it in the list of found entities. 587 void AddOneGenericVariable(NameSearchContext &context, const Symbol &symbol, 588 unsigned int current_id); 589 590 /// Use the NameSearchContext to generate a Decl for the given function. 591 /// (Functions are not placed in the Tuple list.) Can handle both fully 592 /// typed functions and generic functions. 593 /// 594 /// \param[in] context 595 /// The NameSearchContext to use when constructing the Decl. 596 /// 597 /// \param[in] fun 598 /// The Function that needs to be created. If non-NULL, this is 599 /// a fully-typed function. 600 /// 601 /// \param[in] sym 602 /// The Symbol that corresponds to a function that needs to be 603 /// created with generic type (unitptr_t foo(...)). 604 void AddOneFunction(NameSearchContext &context, Function *fun, Symbol *sym, 605 unsigned int current_id); 606 607 /// Use the NameSearchContext to generate a Decl for the given register. 608 /// 609 /// \param[in] context 610 /// The NameSearchContext to use when constructing the Decl. 611 /// 612 /// \param[in] reg_info 613 /// The information corresponding to that register. 614 void AddOneRegister(NameSearchContext &context, const RegisterInfo *reg_info, 615 unsigned int current_id); 616 617 /// Use the NameSearchContext to generate a Decl for the given type. (Types 618 /// are not placed in the Tuple list.) 619 /// 620 /// \param[in] context 621 /// The NameSearchContext to use when constructing the Decl. 622 /// 623 /// \param[in] type 624 /// The type that needs to be created. 625 void AddOneType(NameSearchContext &context, const TypeFromUser &type, 626 unsigned int current_id); 627 628 /// Generate a Decl for "*this" and add a member function declaration to it 629 /// for the expression, then report it. 630 /// 631 /// \param[in] context 632 /// The NameSearchContext to use when constructing the Decl. 633 /// 634 /// \param[in] type 635 /// The type for *this. 636 void AddThisType(NameSearchContext &context, const TypeFromUser &type, 637 unsigned int current_id); 638 639 /// Move a type out of the current ASTContext into another, but make sure to 640 /// export all components of the type also. 641 /// 642 /// \param[in] target 643 /// The TypeSystemClang to move to. 644 /// \param[in] source 645 /// The TypeSystemClang to move from. This is assumed to be going away. 646 /// \param[in] parser_type 647 /// The type as it appears in the source context. 648 /// 649 /// \return 650 /// Returns the moved type, or an empty type if there was a problem. 651 TypeFromUser DeportType(TypeSystemClang &target, TypeSystemClang &source, 652 TypeFromParser parser_type); 653 654 TypeSystemClang *GetTypeSystemClang(); 655 }; 656 657 } // namespace lldb_private 658 659 #endif // liblldb_ClangExpressionDeclMap_h_ 660