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