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