ClangExpressionDeclMap.h revision 3b16eb9d424068446fea9cd0e0fe5e7d435f5b6e
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/Symbol/TaggedASTType.h" 31#include "lldb/Symbol/SymbolContext.h" 32#include "lldb/Target/ExecutionContext.h" 33 34namespace lldb_private { 35 36//---------------------------------------------------------------------- 37/// @class ClangExpressionDeclMap ClangExpressionDeclMap.h "lldb/Expression/ClangExpressionDeclMap.h" 38/// @brief Manages named entities that are defined in LLDB's debug information. 39/// 40/// The Clang parser uses the ClangASTSource as an interface to request named 41/// entities from outside an expression. The ClangASTSource reports back, listing 42/// all possible objects corresponding to a particular name. But it in turn 43/// relies on ClangExpressionDeclMap, which performs several important functions. 44/// 45/// First, it records what variables and functions were looked up and what Decls 46/// were returned for them. 47/// 48/// Second, it constructs a struct on behalf of IRForTarget, recording which 49/// variables should be placed where and relaying this information back so that 50/// IRForTarget can generate context-independent code. 51/// 52/// Third, it "materializes" this struct on behalf of the expression command, 53/// finding the current values of each variable and placing them into the 54/// struct so that it can be passed to the JITted version of the IR. 55/// 56/// Fourth and finally, it "dematerializes" the struct after the JITted code has 57/// has executed, placing the new values back where it found the old ones. 58//---------------------------------------------------------------------- 59class ClangExpressionDeclMap : 60 public ClangASTSource 61{ 62public: 63 //------------------------------------------------------------------ 64 /// Constructor 65 /// 66 /// Initializes class variables. 67 /// 68 /// @param[in] keep_result_in_memory 69 /// If true, inhibits the normal deallocation of the memory for 70 /// the result persistent variable, and instead marks the variable 71 /// as persisting. 72 /// 73 /// @param[in] exe_ctx 74 /// The execution context to use when parsing. 75 //------------------------------------------------------------------ 76 ClangExpressionDeclMap (bool keep_result_in_memory, 77 ExecutionContext &exe_ctx); 78 79 //------------------------------------------------------------------ 80 /// Destructor 81 //------------------------------------------------------------------ 82 ~ClangExpressionDeclMap (); 83 84 //------------------------------------------------------------------ 85 /// Enable the state needed for parsing and IR transformation. 86 /// 87 /// @param[in] exe_ctx 88 /// The execution context to use when finding types for variables. 89 /// Also used to find a "scratch" AST context to store result types. 90 /// 91 /// @param[in] materializer 92 /// If non-NULL, the materializer to populate with information about 93 /// the variables to use 94 /// 95 /// @return 96 /// True if parsing is possible; false if it is unsafe to continue. 97 //------------------------------------------------------------------ 98 bool 99 WillParse (ExecutionContext &exe_ctx, 100 Materializer *materializer); 101 102 //------------------------------------------------------------------ 103 /// [Used by ClangExpressionParser] For each variable that had an unknown 104 /// type at the beginning of parsing, determine its final type now. 105 /// 106 /// @return 107 /// True on success; false otherwise. 108 //------------------------------------------------------------------ 109 bool 110 ResolveUnknownTypes(); 111 112 //------------------------------------------------------------------ 113 /// Disable the state needed for parsing and IR transformation. 114 //------------------------------------------------------------------ 115 void 116 DidParse (); 117 118 //------------------------------------------------------------------ 119 /// [Used by IRForTarget] Get a new result variable name of the form 120 /// $n, where n is a natural number starting with 0. 121 /// 122 /// @param[in] name 123 /// The std::string to place the name into. 124 //------------------------------------------------------------------ 125 const ConstString & 126 GetPersistentResultName (); 127 128 //------------------------------------------------------------------ 129 /// [Used by IRForTarget] Get a constant variable given a name, 130 /// a type, and an llvm::APInt. 131 /// 132 /// @param[in] name 133 /// The name of the variable 134 /// 135 /// @param[in] type 136 /// The type of the variable, which will be imported into the 137 /// target's AST context 138 /// 139 /// @param[in] value 140 /// The value of the variable 141 /// 142 /// @return 143 /// The created variable 144 //------------------------------------------------------------------ 145 lldb::ClangExpressionVariableSP 146 BuildIntegerVariable (const ConstString &name, 147 lldb_private::TypeFromParser type, 148 const llvm::APInt& value); 149 150 //------------------------------------------------------------------ 151 /// [Used by IRForTarget] Cast an existing variable given a Decl and 152 /// a type. 153 /// 154 /// @param[in] name 155 /// The name of the new variable 156 /// 157 /// @param[in] decl 158 /// The Clang variable declaration for the original variable, 159 /// which must be looked up in the map 160 /// 161 /// @param[in] type 162 /// The desired type of the variable after casting 163 /// 164 /// @return 165 /// The created variable 166 //------------------------------------------------------------------ 167 lldb::ClangExpressionVariableSP 168 BuildCastVariable (const ConstString &name, 169 clang::VarDecl *decl, 170 lldb_private::TypeFromParser type); 171 172 //------------------------------------------------------------------ 173 /// [Used by IRForTarget] Add a variable to the list of persistent 174 /// variables for the process. 175 /// 176 /// @param[in] decl 177 /// The Clang declaration for the persistent variable, used for 178 /// lookup during parsing. 179 /// 180 /// @param[in] name 181 /// The name of the persistent variable, usually $something. 182 /// 183 /// @param[in] type 184 /// The type of the variable, in the Clang parser's context. 185 /// 186 /// @return 187 /// True on success; false otherwise. 188 //------------------------------------------------------------------ 189 bool 190 AddPersistentVariable (const clang::NamedDecl *decl, 191 const ConstString &name, 192 TypeFromParser type, 193 bool is_result, 194 bool is_lvalue); 195 196 //------------------------------------------------------------------ 197 /// [Used by IRForTarget] Add a variable to the struct that needs to 198 /// be materialized each time the expression runs. 199 /// 200 /// @param[in] decl 201 /// The Clang declaration for the variable. 202 /// 203 /// @param[in] name 204 /// The name of the variable. 205 /// 206 /// @param[in] value 207 /// The LLVM IR value for this variable. 208 /// 209 /// @param[in] size 210 /// The size of the variable in bytes. 211 /// 212 /// @param[in] alignment 213 /// The required alignment of the variable in bytes. 214 /// 215 /// @return 216 /// True on success; false otherwise. 217 //------------------------------------------------------------------ 218 bool 219 AddValueToStruct (const clang::NamedDecl *decl, 220 const ConstString &name, 221 llvm::Value *value, 222 size_t size, 223 off_t alignment); 224 225 //------------------------------------------------------------------ 226 /// [Used by IRForTarget] Finalize the struct, laying out the position 227 /// of each object in it. 228 /// 229 /// @return 230 /// True on success; false otherwise. 231 //------------------------------------------------------------------ 232 bool 233 DoStructLayout (); 234 235 //------------------------------------------------------------------ 236 /// [Used by IRForTarget] Get general information about the laid-out 237 /// struct after DoStructLayout() has been called. 238 /// 239 /// @param[out] num_elements 240 /// The number of elements in the struct. 241 /// 242 /// @param[out] size 243 /// The size of the struct, in bytes. 244 /// 245 /// @param[out] alignment 246 /// The alignment of the struct, in bytes. 247 /// 248 /// @return 249 /// True if the information could be retrieved; false otherwise. 250 //------------------------------------------------------------------ 251 bool 252 GetStructInfo (uint32_t &num_elements, 253 size_t &size, 254 off_t &alignment); 255 256 //------------------------------------------------------------------ 257 /// [Used by IRForTarget] Get specific information about one field 258 /// of the laid-out struct after DoStructLayout() has been called. 259 /// 260 /// @param[out] decl 261 /// The parsed Decl for the field, as generated by ClangASTSource 262 /// on ClangExpressionDeclMap's behalf. In the case of the result 263 /// value, this will have the name $__lldb_result even if the 264 /// result value ends up having the name $1. This is an 265 /// implementation detail of IRForTarget. 266 /// 267 /// @param[out] value 268 /// The IR value for the field (usually a GlobalVariable). In 269 /// the case of the result value, this will have the correct 270 /// name ($1, for instance). This is an implementation detail 271 /// of IRForTarget. 272 /// 273 /// @param[out] offset 274 /// The offset of the field from the beginning of the struct. 275 /// As long as the struct is aligned according to its required 276 /// alignment, this offset will align the field correctly. 277 /// 278 /// @param[out] name 279 /// The name of the field as used in materialization. 280 /// 281 /// @param[in] index 282 /// The index of the field about which information is requested. 283 /// 284 /// @return 285 /// True if the information could be retrieved; false otherwise. 286 //------------------------------------------------------------------ 287 bool 288 GetStructElement (const clang::NamedDecl *&decl, 289 llvm::Value *&value, 290 off_t &offset, 291 ConstString &name, 292 uint32_t index); 293 294 //------------------------------------------------------------------ 295 /// [Used by IRForTarget] Get information about a function given its 296 /// Decl. 297 /// 298 /// @param[in] decl 299 /// The parsed Decl for the Function, as generated by ClangASTSource 300 /// on ClangExpressionDeclMap's behalf. 301 /// 302 /// @param[out] ptr 303 /// The absolute address of the function in the target. 304 /// 305 /// @return 306 /// True if the information could be retrieved; false otherwise. 307 //------------------------------------------------------------------ 308 bool 309 GetFunctionInfo (const clang::NamedDecl *decl, 310 uint64_t &ptr); 311 312 //------------------------------------------------------------------ 313 /// [Used by IRForTarget] Get the address of a function given nothing 314 /// but its name. Some functions are needed but didn't get Decls made 315 /// during parsing -- specifically, sel_registerName is never called 316 /// in the generated IR but we need to call it nonetheless. 317 /// 318 /// @param[in] name 319 /// The name of the function. 320 /// 321 /// @param[out] ptr 322 /// The absolute address of the function in the target. 323 /// 324 /// @return 325 /// True if the address could be retrieved; false otherwise. 326 //------------------------------------------------------------------ 327 bool 328 GetFunctionAddress (const ConstString &name, 329 uint64_t &ptr); 330 331 //------------------------------------------------------------------ 332 /// [Used by IRForTarget] Get the address of a symbol given nothing 333 /// but its name. 334 /// 335 /// @param[in] target 336 /// The target to find the symbol in. If not provided, 337 /// then the current parsing context's Target. 338 /// 339 /// @param[in] process 340 /// The process to use. For Objective-C symbols, the process's 341 /// Objective-C language runtime may be queried if the process 342 /// is non-NULL. 343 /// 344 /// @param[in] name 345 /// The name of the symbol. 346 /// 347 /// @return 348 /// Valid load address for the symbol 349 //------------------------------------------------------------------ 350 lldb::addr_t 351 GetSymbolAddress (Target &target, 352 Process *process, 353 const ConstString &name, 354 lldb::SymbolType symbol_type); 355 356 lldb::addr_t 357 GetSymbolAddress (const ConstString &name, 358 lldb::SymbolType symbol_type); 359 360 //------------------------------------------------------------------ 361 /// [Used by IRInterpreter] Get basic target information. 362 /// 363 /// @param[out] byte_order 364 /// The byte order of the target. 365 /// 366 /// @param[out] address_byte_size 367 /// The size of a pointer in bytes. 368 /// 369 /// @return 370 /// True if the information could be determined; false 371 /// otherwise. 372 //------------------------------------------------------------------ 373 struct TargetInfo 374 { 375 lldb::ByteOrder byte_order; 376 size_t address_byte_size; 377 378 TargetInfo() : 379 byte_order(lldb::eByteOrderInvalid), 380 address_byte_size(0) 381 { 382 } 383 384 bool IsValid() 385 { 386 return (byte_order != lldb::eByteOrderInvalid && 387 address_byte_size != 0); 388 } 389 }; 390 TargetInfo GetTargetInfo(); 391 392 //------------------------------------------------------------------ 393 /// [Used by IRInterpreter] Promote an unknown address to a 394 /// LoadAddress or FileAddress depending on the presence of a 395 /// process. 396 /// 397 /// @param[in] addr 398 /// The address to promote. 399 /// 400 /// @return 401 /// The wrapped entity. 402 //------------------------------------------------------------------ 403 lldb_private::Value WrapBareAddress (lldb::addr_t addr); 404 405 //------------------------------------------------------------------ 406 /// [Used by IRInterpreter] Write to the target. 407 /// 408 /// @param[in] value 409 /// The address to write to. 410 /// 411 /// @param[in] addr 412 /// The address of the data buffer to read from. 413 /// 414 /// @param[in] length 415 /// The amount of data to write, in bytes. 416 /// 417 /// @return 418 /// True if the write could be performed; false otherwise. 419 //------------------------------------------------------------------ 420 bool 421 WriteTarget (lldb_private::Value &value, 422 const uint8_t *data, 423 size_t length); 424 425 //------------------------------------------------------------------ 426 /// [Used by IRInterpreter] Read from the target. 427 /// 428 /// @param[in] data 429 /// The address of the data buffer to write to. 430 /// 431 /// @param[in] value 432 /// The address to read from. 433 /// 434 /// @param[in] length 435 /// The amount of data to read, in bytes. 436 /// 437 /// @return 438 /// True if the read could be performed; false otherwise. 439 //------------------------------------------------------------------ 440 bool 441 ReadTarget (uint8_t *data, 442 lldb_private::Value &value, 443 size_t length); 444 445 //------------------------------------------------------------------ 446 /// [Used by IRInterpreter] Get the Value for a NamedDecl. 447 /// 448 /// @param[in] decl 449 /// The Decl whose value is to be found. 450 /// 451 /// @param[out] flags 452 /// The flags for the found variable. 453 /// 454 /// @return 455 /// The value, or NULL. 456 //------------------------------------------------------------------ 457 lldb_private::Value 458 LookupDecl (clang::NamedDecl *decl, 459 ClangExpressionVariable::FlagType &flags); 460 461 //------------------------------------------------------------------ 462 /// [Used by IRInterpreter] Get the Value for "this", "self", or 463 /// "_cmd". 464 /// 465 /// @param[in] name 466 /// The name of the entity to be found. 467 /// 468 /// @return 469 /// The value, or NULL. 470 //------------------------------------------------------------------ 471 lldb_private::Value 472 GetSpecialValue (const ConstString &name); 473 474 //------------------------------------------------------------------ 475 /// [Used by IRInterpreter] Returns true if the result is a 476 /// reference to data in the target, meaning it must be 477 /// dereferenced once more to get its data. 478 /// 479 /// @param[in] name 480 /// The name of the result. 481 /// 482 /// @return 483 /// True if the result is a reference; false otherwise (or on 484 /// error). 485 //------------------------------------------------------------------ 486 bool 487 ResultIsReference (const ConstString &name); 488 489 //------------------------------------------------------------------ 490 /// [Used by IRInterpreter] Find the result persistent variable, 491 /// propagate the given value to it, and return it. 492 /// 493 /// @param[out] valobj 494 /// Set to the complete object. 495 /// 496 /// @param[in] value 497 /// A value indicating the location of the value's contents. 498 /// 499 /// @param[in] name 500 /// The name of the result. 501 /// 502 /// @param[in] type 503 /// The type of the data. 504 /// 505 /// @param[in] transient 506 /// True if the data should be treated as disappearing after the 507 /// expression completes. In that case, it gets no live data. 508 /// 509 /// @param[in] maybe_make_load 510 /// True if the value is a file address but should be potentially 511 /// upgraded to a load address if a target is presence. 512 /// 513 /// @return 514 /// True on success; false otherwise. 515 //------------------------------------------------------------------ 516 bool 517 CompleteResultVariable (lldb::ClangExpressionVariableSP &valobj, 518 lldb_private::Value &value, 519 const ConstString &name, 520 lldb_private::TypeFromParser type, 521 bool transient, 522 bool maybe_make_load); 523 524 525 void 526 RemoveResultVariable (const ConstString &name); 527 528 //------------------------------------------------------------------ 529 /// [Used by CommandObjectExpression] Materialize the entire struct 530 /// at a given address, which should be aligned as specified by 531 /// GetStructInfo(). 532 /// 533 /// @param[in] struct_address 534 /// The address at which the struct should be written. 535 /// 536 /// @param[in] error 537 /// An Error to populate with any messages related to 538 /// materializing the struct. 539 /// 540 /// @return 541 /// True on success; false otherwise. 542 //------------------------------------------------------------------ 543 bool 544 Materialize (lldb::addr_t &struct_address, 545 Error &error); 546 547 //------------------------------------------------------------------ 548 /// [Used by CommandObjectExpression] Get the "this" pointer 549 /// from a given execution context. 550 /// 551 /// @param[out] object_ptr 552 /// The this pointer. 553 /// 554 /// @param[in] object_name 555 /// The name of the object pointer -- "this," "self," or similar 556 /// depending on language 557 /// 558 /// @param[in] error 559 /// An Error to populate with any messages related to 560 /// finding the "this" pointer. 561 /// 562 /// @param[in] suppress_type_check 563 /// True if the type is not needed. 564 /// 565 /// @return 566 /// True on success; false otherwise. 567 //------------------------------------------------------------------ 568 bool 569 GetObjectPointer (lldb::addr_t &object_ptr, 570 ConstString &object_name, 571 Error &error, 572 bool suppress_type_check = false); 573 574 //------------------------------------------------------------------ 575 /// [Used by CommandObjectExpression] Pretty-print a materialized 576 /// struct, which must have been materialized by Materialize(), 577 /// byte for byte on a given stream. 578 /// 579 /// @param[in] exe_ctx 580 /// The execution context from which to read the struct. 581 /// 582 /// @param[in] s 583 /// The stream on which to write the pretty-printed output. 584 /// 585 /// @param[in] error 586 /// An Error to populate with any messages related to 587 /// pretty-printing the struct. 588 /// 589 /// @return 590 /// True on success; false otherwise. 591 //------------------------------------------------------------------ 592 bool 593 DumpMaterializedStruct (Stream &s, 594 Error &error); 595 596 //------------------------------------------------------------------ 597 /// [Used by CommandObjectExpression] Deaterialize the entire struct. 598 /// 599 /// @param[in] exe_ctx 600 /// The execution context from which to read the struct. 601 /// 602 /// @param[out] result 603 /// A ClangExpressionVariable containing the result of the 604 /// expression, for potential re-use. 605 /// 606 /// @param[in] stack_frame_top, stack_frame_bottom 607 /// If not LLDB_INVALID_ADDRESS, the bounds for the stack frame 608 /// in which the expression ran. A result whose address falls 609 /// inside this stack frame is dematerialized as a value 610 /// requiring rematerialization. 611 /// 612 /// @param[in] error 613 /// An Error to populate with any messages related to 614 /// dematerializing the struct. 615 /// 616 /// @return 617 /// True on success; false otherwise. 618 //------------------------------------------------------------------ 619 bool 620 Dematerialize (lldb::ClangExpressionVariableSP &result_sp, 621 lldb::addr_t stack_frame_top, 622 lldb::addr_t stack_frame_bottom, 623 Error &error); 624 625 //------------------------------------------------------------------ 626 /// [Used by ClangASTSource] Find all entities matching a given name, 627 /// using a NameSearchContext to make Decls for them. 628 /// 629 /// @param[in] context 630 /// The NameSearchContext that can construct Decls for this name. 631 /// 632 /// @return 633 /// True on success; false otherwise. 634 //------------------------------------------------------------------ 635 void 636 FindExternalVisibleDecls (NameSearchContext &context); 637 638 //------------------------------------------------------------------ 639 /// Find all entities matching a given name in a given module/namespace, 640 /// using a NameSearchContext to make Decls for them. 641 /// 642 /// @param[in] context 643 /// The NameSearchContext that can construct Decls for this name. 644 /// 645 /// @param[in] module 646 /// If non-NULL, the module to query. 647 /// 648 /// @param[in] namespace_decl 649 /// If valid and module is non-NULL, the parent namespace. 650 /// 651 /// @param[in] name 652 /// The name as a plain C string. The NameSearchContext contains 653 /// a DeclarationName for the name so at first the name may seem 654 /// redundant, but ClangExpressionDeclMap operates in RTTI land so 655 /// it can't access DeclarationName. 656 /// 657 /// @param[in] current_id 658 /// The ID for the current FindExternalVisibleDecls invocation, 659 /// for logging purposes. 660 /// 661 /// @return 662 /// True on success; false otherwise. 663 //------------------------------------------------------------------ 664 void 665 FindExternalVisibleDecls (NameSearchContext &context, 666 lldb::ModuleSP module, 667 ClangNamespaceDecl &namespace_decl, 668 unsigned int current_id); 669private: 670 ClangExpressionVariableList m_found_entities; ///< All entities that were looked up for the parser. 671 ClangExpressionVariableList m_struct_members; ///< All entities that need to be placed in the struct. 672 bool m_keep_result_in_memory; ///< True if result persistent variables generated by this expression should stay in memory. 673 674 //---------------------------------------------------------------------- 675 /// The following values should not live beyond parsing 676 //---------------------------------------------------------------------- 677 class ParserVars 678 { 679 public: 680 ParserVars(ClangExpressionDeclMap &decl_map) : 681 m_exe_ctx(), 682 m_sym_ctx(), 683 m_persistent_vars(NULL), 684 m_enable_lookups(false), 685 m_materializer(NULL), 686 m_decl_map(decl_map) 687 { 688 } 689 690 Target * 691 GetTarget() 692 { 693 if (m_exe_ctx.GetTargetPtr()) 694 return m_exe_ctx.GetTargetPtr(); 695 else if (m_sym_ctx.target_sp) 696 m_sym_ctx.target_sp.get(); 697 return NULL; 698 } 699 700 ExecutionContext m_exe_ctx; ///< The execution context to use when parsing. 701 SymbolContext m_sym_ctx; ///< The symbol context to use in finding variables and types. 702 ClangPersistentVariables *m_persistent_vars; ///< The persistent variables for the process. 703 bool m_enable_lookups; ///< Set to true during parsing if we have found the first "$__lldb" name. 704 TargetInfo m_target_info; ///< Basic information about the target. 705 Materializer *m_materializer; ///< If non-NULL, the materializer to use when reporting used variables. 706 private: 707 ClangExpressionDeclMap &m_decl_map; 708 DISALLOW_COPY_AND_ASSIGN (ParserVars); 709 }; 710 711 std::auto_ptr<ParserVars> m_parser_vars; 712 713 //---------------------------------------------------------------------- 714 /// Activate parser-specific variables 715 //---------------------------------------------------------------------- 716 void 717 EnableParserVars() 718 { 719 if (!m_parser_vars.get()) 720 m_parser_vars.reset(new ParserVars(*this)); 721 } 722 723 //---------------------------------------------------------------------- 724 /// Deallocate parser-specific variables 725 //---------------------------------------------------------------------- 726 void 727 DisableParserVars() 728 { 729 m_parser_vars.reset(); 730 } 731 732 //---------------------------------------------------------------------- 733 /// The following values contain layout information for the materialized 734 /// struct, but are not specific to a single materialization 735 //---------------------------------------------------------------------- 736 struct StructVars { 737 StructVars() : 738 m_struct_alignment(0), 739 m_struct_size(0), 740 m_struct_laid_out(false), 741 m_result_name(), 742 m_object_pointer_type(NULL, NULL) 743 { 744 } 745 746 off_t m_struct_alignment; ///< The alignment of the struct in bytes. 747 size_t m_struct_size; ///< The size of the struct in bytes. 748 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). 749 ConstString m_result_name; ///< The name of the result variable ($1, for example) 750 TypeFromUser m_object_pointer_type; ///< The type of the "this" variable, if one exists 751 }; 752 753 std::auto_ptr<StructVars> m_struct_vars; 754 755 //---------------------------------------------------------------------- 756 /// Activate struct variables 757 //---------------------------------------------------------------------- 758 void 759 EnableStructVars() 760 { 761 if (!m_struct_vars.get()) 762 m_struct_vars.reset(new struct StructVars); 763 } 764 765 //---------------------------------------------------------------------- 766 /// Deallocate struct variables 767 //---------------------------------------------------------------------- 768 void 769 DisableStructVars() 770 { 771 m_struct_vars.reset(); 772 } 773 774 //---------------------------------------------------------------------- 775 /// The following values refer to a specific materialization of the 776 /// structure in a process 777 //---------------------------------------------------------------------- 778 struct MaterialVars { 779 MaterialVars() : 780 m_allocated_area(0), 781 m_materialized_location(0) 782 { 783 } 784 785 Process *m_process; ///< The process that the struct is materialized into. 786 lldb::addr_t m_allocated_area; ///< The base of the memory allocated for the struct. Starts on a potentially unaligned address and may therefore be larger than the struct. 787 lldb::addr_t m_materialized_location; ///< The address at which the struct is placed. Falls inside the allocated area. 788 }; 789 790 std::auto_ptr<MaterialVars> m_material_vars; 791 792 //---------------------------------------------------------------------- 793 /// Activate materialization-specific variables 794 //---------------------------------------------------------------------- 795 void 796 EnableMaterialVars() 797 { 798 if (!m_material_vars.get()) 799 m_material_vars.reset(new struct MaterialVars); 800 } 801 802 //---------------------------------------------------------------------- 803 /// Deallocate materialization-specific variables 804 //---------------------------------------------------------------------- 805 void 806 DisableMaterialVars() 807 { 808 m_material_vars.reset(); 809 } 810 811 //---------------------------------------------------------------------- 812 /// Get this parser's ID for use in extracting parser- and JIT-specific 813 /// data from persistent variables. 814 //---------------------------------------------------------------------- 815 uint64_t 816 GetParserID() 817 { 818 return (uint64_t)this; 819 } 820 821 //------------------------------------------------------------------ 822 /// Given a stack frame, find a variable that matches the given name and 823 /// type. We need this for expression re-use; we may not always get the 824 /// same lldb::Variable back, and we want the expression to work wherever 825 /// it can. Returns the variable defined in the tightest scope. 826 /// 827 /// @param[in] frame 828 /// The stack frame to use as a basis for finding the variable. 829 /// 830 /// @param[in] name 831 /// The name as a plain C string. 832 /// 833 /// @param[in] type 834 /// The required type for the variable. This function may be called 835 /// during parsing, in which case we don't know its type; hence the 836 /// default. 837 /// 838 /// @param[in] object_pointer 839 /// The type expected is an object type. This means we will ignore 840 /// constness of the pointer target. 841 /// 842 /// @return 843 /// The LLDB Variable found, or NULL if none was found. 844 //------------------------------------------------------------------ 845 lldb::VariableSP 846 FindVariableInScope (StackFrame &frame, 847 const ConstString &name, 848 TypeFromUser *type = NULL, 849 bool object_pointer = false); 850 851 //------------------------------------------------------------------ 852 /// Given a target, find a data symbol that has the given name. 853 /// 854 /// @param[in] target 855 /// The target to use as the basis for the search. 856 /// 857 /// @param[in] name 858 /// The name as a plain C string. 859 /// 860 /// @return 861 /// The LLDB Symbol found, or NULL if none was found. 862 //--------------------------------------------------------- 863 const Symbol * 864 FindGlobalDataSymbol (Target &target, 865 const ConstString &name); 866 867 //------------------------------------------------------------------ 868 /// Given a target, find a variable that matches the given name and 869 /// type. 870 /// 871 /// @param[in] target 872 /// The target to use as a basis for finding the variable. 873 /// 874 /// @param[in] module 875 /// If non-NULL, the module to search. 876 /// 877 /// @param[in] name 878 /// The name as a plain C string. 879 /// 880 /// @param[in] namespace_decl 881 /// If non-NULL and module is non-NULL, the parent namespace. 882 /// 883 /// @param[in] type 884 /// The required type for the variable. This function may be called 885 /// during parsing, in which case we don't know its type; hence the 886 /// default. 887 /// 888 /// @return 889 /// The LLDB Variable found, or NULL if none was found. 890 //------------------------------------------------------------------ 891 lldb::VariableSP 892 FindGlobalVariable (Target &target, 893 lldb::ModuleSP &module, 894 const ConstString &name, 895 ClangNamespaceDecl *namespace_decl, 896 TypeFromUser *type = NULL); 897 898 //------------------------------------------------------------------ 899 /// Get the value of a variable in a given execution context and return 900 /// the associated Types if needed. 901 /// 902 /// @param[in] var 903 /// The variable to evaluate. 904 /// 905 /// @param[in] parser_ast_context 906 /// The AST context of the parser, to store the found type in. 907 /// 908 /// @param[out] found_type 909 /// The type of the found value, as it was found in the user process. 910 /// This is only useful when the variable is being inspected on behalf 911 /// of the parser, hence the default. 912 /// 913 /// @param[out] parser_type 914 /// The type of the found value, as it was copied into the parser's 915 /// AST context. This is only useful when the variable is being 916 /// inspected on behalf of the parser, hence the default. 917 /// 918 /// @param[in] decl 919 /// The Decl to be looked up. 920 /// 921 /// @return 922 /// The LLDB Value for the variable. 923 //------------------------------------------------------------------ 924 Value * 925 GetVariableValue (lldb::VariableSP &var, 926 clang::ASTContext *parser_ast_context, 927 TypeFromUser *found_type = NULL, 928 TypeFromParser *parser_type = NULL); 929 930 //------------------------------------------------------------------ 931 /// Use the NameSearchContext to generate a Decl for the given LLDB 932 /// Variable, and put it in the Tuple list. 933 /// 934 /// @param[in] context 935 /// The NameSearchContext to use when constructing the Decl. 936 /// 937 /// @param[in] var 938 /// The LLDB Variable that needs a Decl. 939 /// 940 /// @param[in] valobj 941 /// The LLDB ValueObject for that variable. 942 //------------------------------------------------------------------ 943 void 944 AddOneVariable (NameSearchContext &context, 945 lldb::VariableSP var, 946 lldb::ValueObjectSP valobj, 947 unsigned int current_id); 948 949 //------------------------------------------------------------------ 950 /// Use the NameSearchContext to generate a Decl for the given 951 /// persistent variable, and put it in the list of found entities. 952 /// 953 /// @param[in] context 954 /// The NameSearchContext to use when constructing the Decl. 955 /// 956 /// @param[in] pvar 957 /// The persistent variable that needs a Decl. 958 /// 959 /// @param[in] current_id 960 /// The ID of the current invocation of FindExternalVisibleDecls 961 /// for logging purposes. 962 //------------------------------------------------------------------ 963 void 964 AddOneVariable (NameSearchContext &context, 965 lldb::ClangExpressionVariableSP &pvar_sp, 966 unsigned int current_id); 967 968 //------------------------------------------------------------------ 969 /// Use the NameSearchContext to generate a Decl for the given LLDB 970 /// symbol (treated as a variable), and put it in the list of found 971 /// entities. 972 /// 973 /// @param[in] context 974 /// The NameSearchContext to use when constructing the Decl. 975 /// 976 /// @param[in] var 977 /// The LLDB Variable that needs a Decl. 978 //------------------------------------------------------------------ 979 void 980 AddOneGenericVariable (NameSearchContext &context, 981 const Symbol &symbol, 982 unsigned int current_id); 983 984 //------------------------------------------------------------------ 985 /// Use the NameSearchContext to generate a Decl for the given 986 /// function. (Functions are not placed in the Tuple list.) Can 987 /// handle both fully typed functions and generic functions. 988 /// 989 /// @param[in] context 990 /// The NameSearchContext to use when constructing the Decl. 991 /// 992 /// @param[in] fun 993 /// The Function that needs to be created. If non-NULL, this is 994 /// a fully-typed function. 995 /// 996 /// @param[in] sym 997 /// The Symbol that corresponds to a function that needs to be 998 /// created with generic type (unitptr_t foo(...)). 999 //------------------------------------------------------------------ 1000 void 1001 AddOneFunction (NameSearchContext &context, 1002 Function *fun, 1003 Symbol *sym, 1004 unsigned int current_id); 1005 1006 //------------------------------------------------------------------ 1007 /// Use the NameSearchContext to generate a Decl for the given 1008 /// register. 1009 /// 1010 /// @param[in] context 1011 /// The NameSearchContext to use when constructing the Decl. 1012 /// 1013 /// @param[in] reg_info 1014 /// The information corresponding to that register. 1015 //------------------------------------------------------------------ 1016 void 1017 AddOneRegister (NameSearchContext &context, 1018 const RegisterInfo *reg_info, 1019 unsigned int current_id); 1020 1021 //------------------------------------------------------------------ 1022 /// Use the NameSearchContext to generate a Decl for the given 1023 /// type. (Types are not placed in the Tuple list.) 1024 /// 1025 /// @param[in] context 1026 /// The NameSearchContext to use when constructing the Decl. 1027 /// 1028 /// @param[in] type 1029 /// The type that needs to be created. 1030 //------------------------------------------------------------------ 1031 void 1032 AddOneType (NameSearchContext &context, 1033 TypeFromUser &type, 1034 unsigned int current_id); 1035 1036 //------------------------------------------------------------------ 1037 /// Copy a C++ class type into the parser's AST context and add a 1038 /// member function declaration to it for the expression. 1039 /// 1040 /// @param[in] type 1041 /// The type that needs to be created. 1042 //------------------------------------------------------------------ 1043 1044 TypeFromParser 1045 CopyClassType(TypeFromUser &type, 1046 unsigned int current_id); 1047 1048 //------------------------------------------------------------------ 1049 /// Actually do the task of materializing or dematerializing the struct. 1050 /// Since both tasks are very similar, although ClangExpressionDeclMap 1051 /// exposes two functions to the outside, both call DoMaterialize. 1052 /// 1053 /// @param[in] dematerialize 1054 /// True if the struct is to be dematerialized; false if it is to 1055 /// be materialized. 1056 /// 1057 /// @param[in] stack_frame_top, stack_frame_bottom 1058 /// If not LLDB_INVALID_ADDRESS, the bounds for the stack frame 1059 /// in which the expression ran. A result whose address falls 1060 /// inside this stack frame is dematerialized as a value 1061 /// requiring rematerialization. 1062 /// 1063 /// @param[out] result 1064 /// If the struct is being dematerialized, a pointer into which the 1065 /// location of the result persistent variable is placed. If not, 1066 /// NULL. 1067 /// 1068 /// @param[in] err 1069 /// An Error to populate with any messages related to 1070 /// (de)materializing the struct. 1071 /// 1072 /// @return 1073 /// True on success; false otherwise. 1074 //------------------------------------------------------------------ 1075 bool 1076 DoMaterialize (bool dematerialize, 1077 lldb::addr_t stack_frame_top, 1078 lldb::addr_t stack_frame_bottom, 1079 lldb::ClangExpressionVariableSP *result_sp_ptr, 1080 Error &err); 1081 1082 //------------------------------------------------------------------ 1083 /// Clean up the state required to dematerialize the variable. 1084 //------------------------------------------------------------------ 1085 void 1086 DidDematerialize (); 1087 1088 //------------------------------------------------------------------ 1089 /// Actually do the task of materializing or dematerializing a persistent 1090 /// variable. 1091 /// 1092 /// @param[in] dematerialize 1093 /// True if the variable is to be dematerialized; false if it is to 1094 /// be materialized. 1095 /// 1096 /// @param[in] var_sp 1097 /// The persistent variable to materialize 1098 /// 1099 /// @param[in] addr 1100 /// The address at which to materialize the variable. 1101 /// 1102 /// @param[in] stack_frame_top, stack_frame_bottom 1103 /// If not LLDB_INVALID_ADDRESS, the bounds for the stack frame 1104 /// in which the expression ran. A result whose address falls 1105 /// inside this stack frame is dematerialized as a value 1106 /// requiring rematerialization. 1107 /// 1108 /// @param[in] err 1109 /// An Error to populate with any messages related to 1110 /// (de)materializing the persistent variable. 1111 /// 1112 /// @return 1113 /// True on success; false otherwise. 1114 //------------------------------------------------------------------ 1115 bool 1116 DoMaterializeOnePersistentVariable (bool dematerialize, 1117 lldb::ClangExpressionVariableSP &var_sp, 1118 lldb::addr_t addr, 1119 lldb::addr_t stack_frame_top, 1120 lldb::addr_t stack_frame_bottom, 1121 Error &err); 1122 1123 //------------------------------------------------------------------ 1124 /// Create a temporary buffer in the target process to store the value 1125 /// of a persistent variable that would otherwise not be accessible in 1126 /// memory (e.g., register values or constants). 1127 /// 1128 /// @param[in] process 1129 /// The process to use when allocating the memory. 1130 /// 1131 /// @param[in] expr_var 1132 /// The variable whose live data will hold this buffer. 1133 /// 1134 /// @param[in] err 1135 /// An Error to populate with any messages related to 1136 /// allocating the memory. 1137 /// 1138 /// @return 1139 /// True on success; false otherwise. 1140 //------------------------------------------------------------------ 1141 bool 1142 CreateLiveMemoryForExpressionVariable (Process &process, 1143 lldb::ClangExpressionVariableSP &expr_var, 1144 Error &err); 1145 1146 //------------------------------------------------------------------ 1147 /// Delete a temporary buffer created with 1148 /// CreateLiveMemoryForExpressionVariable. 1149 /// 1150 /// @param[in] process 1151 /// The process to use when deallocating the memory. 1152 /// 1153 /// @param[in] expr_var 1154 /// The variable whose live data will hold this buffer. 1155 /// 1156 /// @param[in] err 1157 /// An Error to populate with any messages related to 1158 /// allocating the memory. 1159 /// 1160 /// @return 1161 /// True on success; false otherwise. 1162 //------------------------------------------------------------------ 1163 bool 1164 DeleteLiveMemoryForExpressionVariable (Process &process, 1165 lldb::ClangExpressionVariableSP &expr_var, 1166 Error &err); 1167 1168 //------------------------------------------------------------------ 1169 /// Actually do the task of materializing or dematerializing a 1170 /// variable. 1171 /// 1172 /// @param[in] dematerialize 1173 /// True if the variable is to be dematerialized; false if it is to 1174 /// be materialized. 1175 /// 1176 /// @param[in] sym_ctx 1177 /// The symbol context to use (for looking the variable up). 1178 /// 1179 /// @param[in] expr_var 1180 /// The entity that the expression parser uses for the variable. 1181 /// In case the variable needs to be copied into the target's 1182 /// memory, this location is stored in the variable during 1183 /// materialization and cleared when it is demateralized. 1184 /// 1185 /// @param[in] addr 1186 /// The address at which to materialize the variable. 1187 /// 1188 /// @param[in] err 1189 /// An Error to populate with any messages related to 1190 /// (de)materializing the persistent variable. 1191 /// 1192 /// @return 1193 /// True on success; false otherwise. 1194 //------------------------------------------------------------------ 1195 bool 1196 DoMaterializeOneVariable (bool dematerialize, 1197 const SymbolContext &sym_ctx, 1198 lldb::ClangExpressionVariableSP &expr_var, 1199 lldb::addr_t addr, 1200 Error &err); 1201 1202 //------------------------------------------------------------------ 1203 /// Actually do the task of materializing or dematerializing a 1204 /// register variable. 1205 /// 1206 /// @param[in] dematerialize 1207 /// True if the variable is to be dematerialized; false if it is to 1208 /// be materialized. 1209 /// 1210 /// @param[in] reg_ctx 1211 /// The register context to use. 1212 /// 1213 /// @param[in] reg_info 1214 /// The information for the register to read/write. 1215 /// 1216 /// @param[in] addr 1217 /// The address at which to materialize the variable. 1218 /// 1219 /// @param[in] err 1220 /// An Error to populate with any messages related to 1221 /// (de)materializing the persistent variable. 1222 /// 1223 /// @return 1224 /// True on success; false otherwise. 1225 //------------------------------------------------------------------ 1226 bool 1227 DoMaterializeOneRegister (bool dematerialize, 1228 RegisterContext ®_ctx, 1229 const RegisterInfo ®_info, 1230 lldb::addr_t addr, 1231 Error &err); 1232}; 1233 1234} // namespace lldb_private 1235 1236#endif // liblldb_ClangExpressionDeclMap_h_ 1237