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