IRExecutionUnit.h revision 8202fe26a2a69ccdeab9c49139e9f8c645907dbc
1//===-- IRExecutionUnit.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 lldb_IRExecutionUnit_h_ 11#define lldb_IRExecutionUnit_h_ 12 13// C Includes 14// C++ Includes 15#include <atomic> 16#include <string> 17#include <vector> 18#include <map> 19 20// Other libraries and framework includes 21// Project includes 22#include "lldb/lldb-forward.h" 23#include "lldb/lldb-private.h" 24#include "lldb/Core/ClangForward.h" 25#include "lldb/Core/DataBufferHeap.h" 26#include "llvm/ExecutionEngine/JITMemoryManager.h" 27#include "lldb/Expression/ClangExpression.h" 28#include "lldb/Expression/ClangExpressionParser.h" 29#include "lldb/Host/Mutex.h" 30 31namespace llvm { 32 33class Module; 34class ExecutionEngine; 35 36} 37 38namespace lldb_private { 39 40class Error; 41 42//---------------------------------------------------------------------- 43/// @class IRExecutionUnit IRExecutionUnit.h "lldb/Expression/IRExecutionUnit.h" 44/// @brief Contains the IR and, optionally, JIT-compiled code for a module. 45/// 46/// This class encapsulates the compiled version of an expression, in IR 47/// form (for interpretation purposes) and in raw machine code form (for 48/// execution in the target). 49/// 50/// This object wraps an IR module that comes from the expression parser, 51/// and knows how to use the JIT to make it into executable code. It can 52/// then be used as input to the IR interpreter, or the address of the 53/// executable code can be passed to a thread plan to run in the target. 54/// 55/// This class creates a subclass of LLVM's JITMemoryManager, because that is 56/// how the JIT emits code. Because LLDB needs to move JIT-compiled code 57/// into the target process, the IRExecutionUnit knows how to copy the 58/// emitted code into the target process. 59//---------------------------------------------------------------------- 60class IRExecutionUnit 61{ 62public: 63 //------------------------------------------------------------------ 64 /// Constructor 65 //------------------------------------------------------------------ 66 IRExecutionUnit (std::auto_ptr<llvm::Module> &module_ap, 67 ConstString &name, 68 lldb::ProcessSP process_sp, 69 std::vector<std::string> &cpu_features); 70 71 //------------------------------------------------------------------ 72 /// Destructor 73 //------------------------------------------------------------------ 74 ~IRExecutionUnit(); 75 76 llvm::Module *GetModule() 77 { 78 return m_module; 79 } 80 81 void GetRunnableInfo(Error &error, 82 lldb::addr_t &func_addr, 83 lldb::addr_t &func_end); 84 85 //------------------------------------------------------------------ 86 /// Accessors for IRForTarget and other clients that may want binary 87 /// data placed on their behalf. The binary data is owned by the 88 /// IRExecutionUnit unless the client explicitly chooses to free it. 89 //------------------------------------------------------------------ 90 91 lldb::addr_t WriteNow(const uint8_t *bytes, 92 size_t size, 93 Error &error); 94 95 void FreeNow(lldb::addr_t allocation); 96 97private: 98 //------------------------------------------------------------------ 99 /// Look up the object in m_address_map that contains a given address, 100 /// find where it was copied to, and return the remote address at the 101 /// same offset into the copied entity 102 /// 103 /// @param[in] local_address 104 /// The address in the debugger. 105 /// 106 /// @return 107 /// The address in the target process. 108 //------------------------------------------------------------------ 109 lldb::addr_t 110 GetRemoteAddressForLocal (lldb::addr_t local_address); 111 112 //------------------------------------------------------------------ 113 /// Look up the object in m_address_map that contains a given address, 114 /// find where it was copied to, and return its address range in the 115 /// target process 116 /// 117 /// @param[in] local_address 118 /// The address in the debugger. 119 /// 120 /// @return 121 /// The range of the containing object in the target process. 122 //------------------------------------------------------------------ 123 typedef std::pair <lldb::addr_t, uintptr_t> AddrRange; 124 AddrRange 125 GetRemoteRangeForLocal (lldb::addr_t local_address); 126 127 //------------------------------------------------------------------ 128 /// Commit all allocations to the process and record where they were stored. 129 /// 130 /// @param[in] process 131 /// The process to allocate memory in. 132 /// 133 /// @return 134 /// True <=> all allocations were performed successfully. 135 /// This method will attempt to free allocated memory if the 136 /// operation fails. 137 //------------------------------------------------------------------ 138 bool 139 CommitAllocations (lldb::ProcessSP &process_sp); 140 141 //------------------------------------------------------------------ 142 /// Report all committed allocations to the execution engine. 143 /// 144 /// @param[in] engine 145 /// The execution engine to notify. 146 //------------------------------------------------------------------ 147 void 148 ReportAllocations (llvm::ExecutionEngine &engine); 149 150 //------------------------------------------------------------------ 151 /// Write the contents of all allocations to the process. 152 /// 153 /// @param[in] local_address 154 /// The process containing the allocations. 155 /// 156 /// @return 157 /// True <=> all allocations were performed successfully. 158 //------------------------------------------------------------------ 159 bool 160 WriteData (lldb::ProcessSP &process_sp); 161 162 Error 163 DisassembleFunction (Stream &stream, 164 lldb::ProcessSP &process_sp); 165 166 class MemoryManager : public llvm::JITMemoryManager 167 { 168 public: 169 MemoryManager (IRExecutionUnit &parent); 170 171 //------------------------------------------------------------------ 172 /// Passthrough interface stub 173 //------------------------------------------------------------------ 174 virtual void setMemoryWritable (); 175 176 //------------------------------------------------------------------ 177 /// Passthrough interface stub 178 //------------------------------------------------------------------ 179 virtual void setMemoryExecutable (); 180 181 //------------------------------------------------------------------ 182 /// Passthrough interface stub 183 //------------------------------------------------------------------ 184 virtual void setPoisonMemory (bool poison) 185 { 186 m_default_mm_ap->setPoisonMemory (poison); 187 } 188 189 //------------------------------------------------------------------ 190 /// Passthrough interface stub 191 //------------------------------------------------------------------ 192 virtual void AllocateGOT() 193 { 194 m_default_mm_ap->AllocateGOT(); 195 } 196 197 //------------------------------------------------------------------ 198 /// Passthrough interface stub 199 //------------------------------------------------------------------ 200 virtual uint8_t *getGOTBase() const 201 { 202 return m_default_mm_ap->getGOTBase(); 203 } 204 205 //------------------------------------------------------------------ 206 /// Passthrough interface stub 207 //------------------------------------------------------------------ 208 virtual uint8_t *startFunctionBody(const llvm::Function *F, 209 uintptr_t &ActualSize); 210 211 //------------------------------------------------------------------ 212 /// Allocate room for a dyld stub for a lazy-referenced function, 213 /// and add it to the m_stubs map 214 /// 215 /// @param[in] F 216 /// The function being referenced. 217 /// 218 /// @param[in] StubSize 219 /// The size of the stub. 220 /// 221 /// @param[in] Alignment 222 /// The required alignment of the stub. 223 /// 224 /// @return 225 /// Allocated space for the stub. 226 //------------------------------------------------------------------ 227 virtual uint8_t *allocateStub(const llvm::GlobalValue* F, 228 unsigned StubSize, 229 unsigned Alignment); 230 231 //------------------------------------------------------------------ 232 /// Complete the body of a function, and add it to the m_functions map 233 /// 234 /// @param[in] F 235 /// The function being completed. 236 /// 237 /// @param[in] FunctionStart 238 /// The first instruction of the function. 239 /// 240 /// @param[in] FunctionEnd 241 /// The last byte of the last instruction of the function. 242 //------------------------------------------------------------------ 243 virtual void endFunctionBody(const llvm::Function *F, 244 uint8_t *FunctionStart, 245 uint8_t *FunctionEnd); 246 //------------------------------------------------------------------ 247 /// Allocate space for an unspecified purpose, and add it to the 248 /// m_spaceBlocks map 249 /// 250 /// @param[in] Size 251 /// The size of the area. 252 /// 253 /// @param[in] Alignment 254 /// The required alignment of the area. 255 /// 256 /// @return 257 /// Allocated space. 258 //------------------------------------------------------------------ 259 virtual uint8_t *allocateSpace(intptr_t Size, unsigned Alignment); 260 261 //------------------------------------------------------------------ 262 /// Allocate space for executable code, and add it to the 263 /// m_spaceBlocks map 264 /// 265 /// @param[in] Size 266 /// The size of the area. 267 /// 268 /// @param[in] Alignment 269 /// The required alignment of the area. 270 /// 271 /// @param[in] SectionID 272 /// A unique identifier for the section. 273 /// 274 /// @return 275 /// Allocated space. 276 //------------------------------------------------------------------ 277 virtual uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment, 278 unsigned SectionID); 279 280 //------------------------------------------------------------------ 281 /// Allocate space for data, and add it to the m_spaceBlocks map 282 /// 283 /// @param[in] Size 284 /// The size of the area. 285 /// 286 /// @param[in] Alignment 287 /// The required alignment of the area. 288 /// 289 /// @param[in] SectionID 290 /// A unique identifier for the section. 291 /// 292 /// @param[in] IsReadOnly 293 /// Flag indicating the section is read-only. 294 /// 295 /// @return 296 /// Allocated space. 297 //------------------------------------------------------------------ 298 virtual uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment, 299 unsigned SectionID, bool IsReadOnly); 300 301 //------------------------------------------------------------------ 302 /// Allocate space for a global variable, and add it to the 303 /// m_spaceBlocks map 304 /// 305 /// @param[in] Size 306 /// The size of the variable. 307 /// 308 /// @param[in] Alignment 309 /// The required alignment of the variable. 310 /// 311 /// @return 312 /// Allocated space for the global. 313 //------------------------------------------------------------------ 314 virtual uint8_t *allocateGlobal(uintptr_t Size, 315 unsigned Alignment); 316 317 //------------------------------------------------------------------ 318 /// Called when object loading is complete and section page 319 /// permissions can be applied. Currently unimplemented for LLDB. 320 /// 321 /// @param[out] ErrMsg 322 /// The error that prevented the page protection from succeeding. 323 /// 324 /// @return 325 /// True in case of failure, false in case of success. 326 //------------------------------------------------------------------ 327 bool applyPermissions(std::string *ErrMsg) { return false; } 328 329 //------------------------------------------------------------------ 330 /// Passthrough interface stub 331 //------------------------------------------------------------------ 332 virtual void deallocateFunctionBody(void *Body); 333 334 //------------------------------------------------------------------ 335 /// Passthrough interface stub 336 //------------------------------------------------------------------ 337 virtual uint8_t* startExceptionTable(const llvm::Function* F, 338 uintptr_t &ActualSize); 339 340 //------------------------------------------------------------------ 341 /// Complete the exception table for a function, and add it to the 342 /// m_exception_tables map 343 /// 344 /// @param[in] F 345 /// The function whose exception table is being written. 346 /// 347 /// @param[in] TableStart 348 /// The first byte of the exception table. 349 /// 350 /// @param[in] TableEnd 351 /// The last byte of the exception table. 352 /// 353 /// @param[in] FrameRegister 354 /// I don't know what this does, but it's passed through. 355 //------------------------------------------------------------------ 356 virtual void endExceptionTable(const llvm::Function *F, 357 uint8_t *TableStart, 358 uint8_t *TableEnd, 359 uint8_t* FrameRegister); 360 361 //------------------------------------------------------------------ 362 /// Passthrough interface stub 363 //------------------------------------------------------------------ 364 virtual void deallocateExceptionTable(void *ET); 365 366 //------------------------------------------------------------------ 367 /// Passthrough interface stub 368 //------------------------------------------------------------------ 369 virtual size_t GetDefaultCodeSlabSize() { 370 return m_default_mm_ap->GetDefaultCodeSlabSize(); 371 } 372 373 //------------------------------------------------------------------ 374 /// Passthrough interface stub 375 //------------------------------------------------------------------ 376 virtual size_t GetDefaultDataSlabSize() { 377 return m_default_mm_ap->GetDefaultDataSlabSize(); 378 } 379 380 virtual size_t GetDefaultStubSlabSize() { 381 return m_default_mm_ap->GetDefaultStubSlabSize(); 382 } 383 384 //------------------------------------------------------------------ 385 /// Passthrough interface stub 386 //------------------------------------------------------------------ 387 virtual unsigned GetNumCodeSlabs() { 388 return m_default_mm_ap->GetNumCodeSlabs(); 389 } 390 391 //------------------------------------------------------------------ 392 /// Passthrough interface stub 393 //------------------------------------------------------------------ 394 virtual unsigned GetNumDataSlabs() { 395 return m_default_mm_ap->GetNumDataSlabs(); 396 } 397 398 //------------------------------------------------------------------ 399 /// Passthrough interface stub 400 //------------------------------------------------------------------ 401 virtual unsigned GetNumStubSlabs() { 402 return m_default_mm_ap->GetNumStubSlabs(); 403 } 404 405 //------------------------------------------------------------------ 406 /// Passthrough interface stub 407 //------------------------------------------------------------------ 408 virtual void *getPointerToNamedFunction(const std::string &Name, 409 bool AbortOnFailure = true) { 410 return m_default_mm_ap->getPointerToNamedFunction(Name, AbortOnFailure); 411 } 412 private: 413 std::auto_ptr<JITMemoryManager> m_default_mm_ap; ///< The memory allocator to use in actually creating space. All calls are passed through to it. 414 IRExecutionUnit &m_parent; ///< The execution unit this is a proxy for. 415 }; 416 417 //---------------------------------------------------------------------- 418 /// @class Allocation IRExecutionUnit.h "lldb/Expression/IRExecutionUnit.h" 419 /// @brief A record of a region that has been allocated by the JIT. 420 /// 421 /// The IRExecutionUnit makes records of all regions that need copying; 422 /// upon requests, it allocates and 423 //---------------------------------------------------------------------- 424 struct Allocation 425 { 426 lldb::addr_t m_remote_allocation;///< The (unaligned) base for the remote allocation 427 lldb::addr_t m_remote_start; ///< The base address of the remote allocation 428 uintptr_t m_local_start; ///< The base address of the local allocation 429 uintptr_t m_size; ///< The size of the allocation 430 unsigned m_section_id; ///< The ID of the section 431 unsigned m_alignment; ///< The required alignment for the allocation 432 bool m_executable; ///< True <=> the allocation must be executable in the target 433 bool m_allocated; ///< True <=> the allocation has been propagated to the target 434 435 std::unique_ptr<DataBufferHeap> m_data; ///< If non-NULL, a local data buffer containing the written bytes. Only populated by WriteNow. 436 437 static const unsigned eSectionIDNone = (unsigned)-1; 438 439 //------------------------------------------------------------------ 440 /// Constructor 441 //------------------------------------------------------------------ 442 Allocation () : 443 m_remote_allocation(0), 444 m_remote_start(0), 445 m_local_start(0), 446 m_size(0), 447 m_section_id(eSectionIDNone), 448 m_alignment(0), 449 m_executable(false), 450 m_allocated(false) 451 { 452 } 453 454 void dump (lldb::LogSP log); 455 }; 456 457 //---------------------------------------------------------------------- 458 /// @class JittedFunction ClangExpressionParser.h "lldb/Expression/ClangExpressionParser.h" 459 /// @brief Encapsulates a single function that has been generated by the JIT. 460 /// 461 /// Functions that have been generated by the JIT are first resident in the 462 /// local process, and then placed in the target process. JittedFunction 463 /// represents a function possibly resident in both. 464 //---------------------------------------------------------------------- 465 struct JittedFunction { 466 std::string m_name; ///< The function's name 467 lldb::addr_t m_local_addr; ///< The address of the function in LLDB's memory 468 lldb::addr_t m_remote_addr; ///< The address of the function in the target's memory 469 470 //------------------------------------------------------------------ 471 /// Constructor 472 /// 473 /// Initializes class variabes. 474 /// 475 /// @param[in] name 476 /// The name of the function. 477 /// 478 /// @param[in] local_addr 479 /// The address of the function in LLDB, or LLDB_INVALID_ADDRESS if 480 /// it is not present in LLDB's memory. 481 /// 482 /// @param[in] remote_addr 483 /// The address of the function in the target, or LLDB_INVALID_ADDRESS 484 /// if it is not present in the target's memory. 485 //------------------------------------------------------------------ 486 JittedFunction (const char *name, 487 lldb::addr_t local_addr = LLDB_INVALID_ADDRESS, 488 lldb::addr_t remote_addr = LLDB_INVALID_ADDRESS) : 489 m_name (name), 490 m_local_addr (local_addr), 491 m_remote_addr (remote_addr) 492 { 493 } 494 }; 495 496 lldb::ProcessWP m_process_wp; 497 typedef std::vector<Allocation> AllocationList; 498 AllocationList m_allocations; ///< The base address of the remote allocation 499 std::auto_ptr<llvm::ExecutionEngine> m_execution_engine_ap; 500 std::auto_ptr<llvm::Module> m_module_ap; ///< Holder for the module until it's been handed off 501 llvm::Module *m_module; ///< Owned by the execution engine 502 std::vector<std::string> m_cpu_features; 503 llvm::SmallVector<JittedFunction, 1> m_jitted_functions; ///< A vector of all functions that have been JITted into machine code 504 const ConstString m_name; 505 506 std::atomic<bool> m_did_jit; 507 Mutex m_jit_mutex; 508 509 lldb::addr_t m_function_load_addr; 510 lldb::addr_t m_function_end_load_addr; 511}; 512 513} // namespace lldb_private 514 515#endif // lldb_IRExecutionUnit_h_ 516