LLDB mainline
IRExecutionUnit.h
Go to the documentation of this file.
1//===-- IRExecutionUnit.h ---------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLDB_EXPRESSION_IREXECUTIONUNIT_H
10#define LLDB_EXPRESSION_IREXECUTIONUNIT_H
11
12#include <atomic>
13#include <memory>
14#include <string>
15#include <vector>
16
17#include "llvm/ExecutionEngine/SectionMemoryManager.h"
18#include "llvm/IR/Module.h"
19
25#include "lldb/lldb-forward.h"
26#include "lldb/lldb-private.h"
27
28namespace llvm {
29
30class Module;
31class ExecutionEngine;
32class ObjectCache;
33
34} // namespace llvm
35
36namespace lldb_private {
37
38class Status;
39
40/// \class IRExecutionUnit IRExecutionUnit.h
41/// "lldb/Expression/IRExecutionUnit.h" Contains the IR and, optionally, JIT-
42/// compiled code for a module.
43///
44/// This class encapsulates the compiled version of an expression, in IR form
45/// (for interpretation purposes) and in raw machine code form (for execution
46/// in the target).
47///
48/// This object wraps an IR module that comes from the expression parser, and
49/// knows how to use the JIT to make it into executable code. It can then be
50/// used as input to the IR interpreter, or the address of the executable code
51/// can be passed to a thread plan to run in the target.
52///
53/// This class creates a subclass of LLVM's SectionMemoryManager, because that
54/// is how the JIT emits code. Because LLDB needs to move JIT-compiled code
55/// into the target process, the IRExecutionUnit knows how to copy the emitted
56/// code into the target process.
57class IRExecutionUnit : public std::enable_shared_from_this<IRExecutionUnit>,
58 public IRMemoryMap,
60public:
61 /// Constructor
62 IRExecutionUnit(std::unique_ptr<llvm::LLVMContext> &context_up,
63 std::unique_ptr<llvm::Module> &module_up, ConstString &name,
64 const lldb::TargetSP &target_sp, const SymbolContext &sym_ctx,
65 std::vector<std::string> &cpu_features);
66
67 /// Destructor
68 ~IRExecutionUnit() override;
69
71
72 llvm::Module *GetModule() { return m_module; }
73
74 llvm::Function *GetFunction() {
75 return ((m_module != nullptr) ? m_module->getFunction(m_name.GetStringRef())
76 : nullptr);
77 }
78
79 void GetRunnableInfo(Status &error, lldb::addr_t &func_addr,
80 lldb::addr_t &func_end);
81
82 /// ObjectFileJITDelegate overrides
83 lldb::ByteOrder GetByteOrder() const override;
84
85 uint32_t GetAddressByteSize() const override;
86
88 lldb_private::Symtab &symtab) override;
89
91 lldb_private::SectionList &section_list) override;
92
93 ArchSpec GetArchitecture() override;
94
95 lldb::TargetSP GetTargetSP() override { return GetTarget(); }
96
98
99 lldb::addr_t FindSymbol(ConstString name, bool &missing_weak);
100
101 void GetStaticInitializers(std::vector<lldb::addr_t> &static_initializers);
102
103 /// \class JittedFunction IRExecutionUnit.h
104 /// "lldb/Expression/IRExecutionUnit.h"
105 /// Encapsulates a single function that has been generated by the JIT.
106 ///
107 /// Functions that have been generated by the JIT are first resident in the
108 /// local process, and then placed in the target process. JittedFunction
109 /// represents a function possibly resident in both.
111 ConstString m_name; ///< The function's name
112 lldb::addr_t m_local_addr; ///< The address of the function in LLDB's memory
114 m_remote_addr; ///< The address of the function in the target's memory
115
116 /// Constructor
117 ///
118 /// Initializes class variabes.
119 ///
120 /// \param[in] name
121 /// The name of the function.
122 ///
123 /// \param[in] local_addr
124 /// The address of the function in LLDB, or LLDB_INVALID_ADDRESS if
125 /// it is not present in LLDB's memory.
126 ///
127 /// \param[in] remote_addr
128 /// The address of the function in the target, or LLDB_INVALID_ADDRESS
129 /// if it is not present in the target's memory.
130 JittedEntity(const char *name,
133 : m_name(name), m_local_addr(local_addr), m_remote_addr(remote_addr) {}
134 };
135
138 JittedFunction(const char *name, bool external,
141 : JittedEntity(name, local_addr, remote_addr), m_external(external) {}
142 };
143
145 JittedGlobalVariable(const char *name,
148 : JittedEntity(name, local_addr, remote_addr) {}
149 };
150
151 const std::vector<JittedFunction> &GetJittedFunctions() {
152 return m_jitted_functions;
153 }
154
155 const std::vector<JittedGlobalVariable> &GetJittedGlobalVariables() {
157 }
158
160 for (auto const &ctx : contexts)
161 if (ctx.module_sp)
162 m_preferred_modules.Append(ctx.module_sp);
163 }
164
165private:
166 /// Look up the object in m_address_map that contains a given address, find
167 /// where it was copied to, and return the remote address at the same offset
168 /// into the copied entity
169 ///
170 /// \param[in] local_address
171 /// The address in the debugger.
172 ///
173 /// \return
174 /// The address in the target process.
176
177 typedef std::pair<lldb::addr_t, uintptr_t> AddrRange;
178
179 /// Look up the object in m_address_map that contains a given address, find
180 /// where it was copied to, and return its address range in the target
181 /// process
182 ///
183 /// \param[in] local_address
184 /// The address in the debugger.
185 ///
186 /// \return
187 /// The range of the containing object in the target process.
189
190 /// Commit all allocations to the process and record where they were stored.
191 ///
192 /// \param[in] process_sp
193 /// The process to allocate memory in.
194 ///
195 /// \return
196 /// True <=> all allocations were performed successfully.
197 /// This method will attempt to free allocated memory if the
198 /// operation fails.
199 bool CommitAllocations(lldb::ProcessSP &process_sp);
200
201 /// Report all committed allocations to the execution engine.
202 ///
203 /// \param[in] engine
204 /// The execution engine to notify.
205 void ReportAllocations(llvm::ExecutionEngine &engine);
206
207 /// Write the contents of all allocations to the process.
208 ///
209 /// \param[in] process_sp
210 /// The process containing the allocations.
211 ///
212 /// \return
213 /// True <=> all allocations were performed successfully.
214 bool WriteData(lldb::ProcessSP &process_sp);
215
216 Status DisassembleFunction(Stream &stream, lldb::ProcessSP &process_sp);
217
218 void CollectCandidateCNames(std::vector<ConstString> &C_names,
219 ConstString name);
220
221 void CollectCandidateCPlusPlusNames(std::vector<ConstString> &CPP_names,
222 const std::vector<ConstString> &C_names,
223 const SymbolContext &sc);
224
225 lldb::addr_t FindInSymbols(const std::vector<ConstString> &names,
227 bool &symbol_was_missing_weak);
228
229 lldb::addr_t FindInRuntimes(const std::vector<ConstString> &names,
231
232 lldb::addr_t FindInUserDefinedSymbols(const std::vector<ConstString> &names,
234
236
237 class MemoryManager : public llvm::SectionMemoryManager {
238 public:
240
241 ~MemoryManager() override;
242
243 /// Allocate space for executable code, and add it to the m_spaceBlocks
244 /// map
245 ///
246 /// \param[in] Size
247 /// The size of the area.
248 ///
249 /// \param[in] Alignment
250 /// The required alignment of the area.
251 ///
252 /// \param[in] SectionID
253 /// A unique identifier for the section.
254 ///
255 /// \return
256 /// Allocated space.
257 uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
258 unsigned SectionID,
259 llvm::StringRef SectionName) override;
260
261 /// Allocate space for data, and add it to the m_spaceBlocks map
262 ///
263 /// \param[in] Size
264 /// The size of the area.
265 ///
266 /// \param[in] Alignment
267 /// The required alignment of the area.
268 ///
269 /// \param[in] SectionID
270 /// A unique identifier for the section.
271 ///
272 /// \param[in] IsReadOnly
273 /// Flag indicating the section is read-only.
274 ///
275 /// \return
276 /// Allocated space.
277 uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
278 unsigned SectionID,
279 llvm::StringRef SectionName,
280 bool IsReadOnly) override;
281
282 /// Called when object loading is complete and section page permissions
283 /// can be applied. Currently unimplemented for LLDB.
284 ///
285 /// \param[out] ErrMsg
286 /// The error that prevented the page protection from succeeding.
287 ///
288 /// \return
289 /// True in case of failure, false in case of success.
290 bool finalizeMemory(std::string *ErrMsg) override {
291 // TODO: Ensure that the instruction cache is flushed because
292 // relocations are updated by dy-load. See:
293 // sys::Memory::InvalidateInstructionCache
294 // llvm::SectionMemoryManager
295 return false;
296 }
297
298 // Ignore any EHFrame registration.
299 void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,
300 size_t Size) override {}
301 void deregisterEHFrames() override {}
302
303 uint64_t getSymbolAddress(const std::string &Name) override;
304
305 // Find the address of the symbol Name. If Name is a missing weak symbol
306 // then missing_weak will be true.
307 uint64_t GetSymbolAddressAndPresence(const std::string &Name,
308 bool &missing_weak);
309
310 llvm::JITSymbol findSymbol(const std::string &Name) override;
311
312 void *getPointerToNamedFunction(const std::string &Name,
313 bool AbortOnFailure = true) override;
314
315 private:
316 std::unique_ptr<SectionMemoryManager> m_default_mm_up; ///< The memory
317 /// allocator to use
318 /// in actually
319 /// creating space.
320 /// All calls are
321 /// passed through to
322 /// it.
323 IRExecutionUnit &m_parent; ///< The execution unit this is a proxy for.
324 };
325
326 static const unsigned eSectionIDInvalid = (unsigned)-1;
327
329
330 static lldb::SectionType
331 GetSectionTypeFromSectionName(const llvm::StringRef &name,
332 AllocationKind alloc_kind);
333
334 /// Encapsulates a single allocation request made by the JIT.
335 ///
336 /// Allocations made by the JIT are first queued up and then applied in bulk
337 /// to the underlying process.
339 std::string m_name;
341 uintptr_t m_host_address;
344 size_t m_size;
345 unsigned m_alignment;
346 unsigned m_section_id;
347
348 AllocationRecord(uintptr_t host_address, uint32_t permissions,
349 lldb::SectionType sect_type, size_t size,
350 unsigned alignment, unsigned section_id, const char *name)
352 m_permissions(permissions), m_sect_type(sect_type), m_size(size),
353 m_alignment(alignment), m_section_id(section_id) {
354 if (name && name[0])
355 m_name = name;
356 }
357
358 void dump(Log *log);
359 };
360
362 AllocationRecord &record);
363
364 typedef std::vector<AllocationRecord> RecordVector;
366
367 std::unique_ptr<llvm::LLVMContext> m_context_up;
368 std::unique_ptr<llvm::ExecutionEngine> m_execution_engine_up;
369 std::unique_ptr<llvm::ObjectCache> m_object_cache_up;
370 std::unique_ptr<llvm::Module>
371 m_module_up; ///< Holder for the module until it's been handed off
372 llvm::Module *m_module; ///< Owned by the execution engine
373 std::vector<std::string> m_cpu_features;
374 std::vector<JittedFunction> m_jitted_functions; ///< A vector of all functions
375 ///that have been JITted into
376 ///machine code
377 std::vector<JittedGlobalVariable> m_jitted_global_variables; ///< A vector of
378 ///all functions
379 ///that have been
380 ///JITted into
381 ///machine code
383 SymbolContext m_sym_ctx; ///< Used for symbol lookups
384 std::vector<ConstString> m_failed_lookups;
385
386 std::atomic<bool> m_did_jit;
387
390
391 bool m_strip_underscore = true; ///< True for platforms where global symbols
392 /// have a _ prefix
393 bool m_reported_allocations; ///< True after allocations have been reported.
394 ///It is possible that
395 ///< sections will be allocated when this is true, in which case they weren't
396 ///< depended on by any function. (Top-level code defining a variable, but
397 ///< defining no functions using that variable, would do this.) If this
398 ///< is true, any allocations need to be committed immediately -- no
399 ///< opportunity for relocation.
400
401 ///< Any Module in this list will be used for symbol/function lookup
402 ///< before any other module (except for the module corresponding to the
403 ///< current frame).
405};
406
407} // namespace lldb_private
408
409#endif // LLDB_EXPRESSION_IREXECUTIONUNIT_H
static llvm::raw_ostream & error(Stream &strm)
An architecture specification class.
Definition ArchSpec.h:32
A uniqued constant string class.
Definition ConstString.h:40
void * getPointerToNamedFunction(const std::string &Name, bool AbortOnFailure=true) override
void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size) override
bool finalizeMemory(std::string *ErrMsg) override
Called when object loading is complete and section page permissions can be applied.
std::unique_ptr< SectionMemoryManager > m_default_mm_up
The memory allocator to use in actually creating space.
uint64_t getSymbolAddress(const std::string &Name) override
uint8_t * allocateDataSection(uintptr_t Size, unsigned Alignment, unsigned SectionID, llvm::StringRef SectionName, bool IsReadOnly) override
Allocate space for data, and add it to the m_spaceBlocks map.
IRExecutionUnit & m_parent
The execution unit this is a proxy for.
uint64_t GetSymbolAddressAndPresence(const std::string &Name, bool &missing_weak)
llvm::JITSymbol findSymbol(const std::string &Name) override
uint8_t * allocateCodeSection(uintptr_t Size, unsigned Alignment, unsigned SectionID, llvm::StringRef SectionName) override
Allocate space for executable code, and add it to the m_spaceBlocks map.
std::vector< std::string > m_cpu_features
void CollectCandidateCPlusPlusNames(std::vector< ConstString > &CPP_names, const std::vector< ConstString > &C_names, const SymbolContext &sc)
static const unsigned eSectionIDInvalid
lldb::TargetSP GetTargetSP() override
Returns the Target this JIT-compiled code was produced for, or null if the delegate that created this...
void PopulateSymtab(lldb_private::ObjectFile *obj_file, lldb_private::Symtab &symtab) override
lldb::addr_t FindInSymbols(const std::vector< ConstString > &names, const lldb_private::SymbolContext &sc, bool &symbol_was_missing_weak)
void GetRunnableInfo(Status &error, lldb::addr_t &func_addr, lldb::addr_t &func_end)
lldb::ByteOrder GetByteOrder() const override
ObjectFileJITDelegate overrides.
SymbolContext m_sym_ctx
Used for symbol lookups.
std::vector< JittedFunction > m_jitted_functions
A vector of all functions that have been JITted into machine code.
Status DisassembleFunction(Stream &stream, lldb::ProcessSP &process_sp)
std::vector< ConstString > m_failed_lookups
std::unique_ptr< llvm::Module > m_module_up
Holder for the module until it's been handed off.
lldb::addr_t FindSymbol(ConstString name, bool &missing_weak)
lldb::addr_t FindInRuntimes(const std::vector< ConstString > &names, const lldb_private::SymbolContext &sc)
IRExecutionUnit(std::unique_ptr< llvm::LLVMContext > &context_up, std::unique_ptr< llvm::Module > &module_up, ConstString &name, const lldb::TargetSP &target_sp, const SymbolContext &sym_ctx, std::vector< std::string > &cpu_features)
Constructor.
static lldb::SectionType GetSectionTypeFromSectionName(const llvm::StringRef &name, AllocationKind alloc_kind)
uint32_t GetAddressByteSize() const override
bool CommitAllocations(lldb::ProcessSP &process_sp)
Commit all allocations to the process and record where they were stored.
bool CommitOneAllocation(lldb::ProcessSP &process_sp, Status &error, AllocationRecord &record)
void GetStaticInitializers(std::vector< lldb::addr_t > &static_initializers)
void CollectCandidateCNames(std::vector< ConstString > &C_names, ConstString name)
std::unique_ptr< llvm::ExecutionEngine > m_execution_engine_up
llvm::Module * m_module
Owned by the execution engine.
std::unique_ptr< llvm::LLVMContext > m_context_up
void AppendPreferredSymbolContexts(SymbolContextList const &contexts)
const std::vector< JittedGlobalVariable > & GetJittedGlobalVariables()
AddrRange GetRemoteRangeForLocal(lldb::addr_t local_address)
Look up the object in m_address_map that contains a given address, find where it was copied to,...
bool m_strip_underscore
True for platforms where global symbols have a _ prefix.
void ReportSymbolLookupError(ConstString name)
lldb::addr_t FindInUserDefinedSymbols(const std::vector< ConstString > &names, const lldb_private::SymbolContext &sc)
void PopulateSectionList(lldb_private::ObjectFile *obj_file, lldb_private::SectionList &section_list) override
std::vector< AllocationRecord > RecordVector
const std::vector< JittedFunction > & GetJittedFunctions()
std::pair< lldb::addr_t, uintptr_t > AddrRange
std::vector< JittedGlobalVariable > m_jitted_global_variables
A vector of all functions that have been JITted into machine code.
bool WriteData(lldb::ProcessSP &process_sp)
Write the contents of all allocations to the process.
std::unique_ptr< llvm::ObjectCache > m_object_cache_up
~IRExecutionUnit() override
Destructor.
void ReportAllocations(llvm::ExecutionEngine &engine)
Report all committed allocations to the execution engine.
lldb::addr_t GetRemoteAddressForLocal(lldb::addr_t local_address)
Look up the object in m_address_map that contains a given address, find where it was copied to,...
bool m_reported_allocations
True after allocations have been reported.
lldb::TargetSP GetTarget()
Definition IRMemoryMap.h:86
IRMemoryMap(lldb::TargetSP target_sp)
A collection class for Module objects.
Definition ModuleList.h:128
A plug-in interface definition class for object file parsers.
Definition ObjectFile.h:46
An error handling class.
Definition Status.h:118
A stream class that can stream formatted output to a file.
Definition Stream.h:28
Defines a list of symbol context objects.
Defines a symbol context baton that can be handed other debug core functions.
#define LLDB_INVALID_ADDRESS
A class that represents a running process on the host machine.
std::shared_ptr< lldb_private::Process > ProcessSP
ByteOrder
Byte ordering definitions.
uint64_t addr_t
Definition lldb-types.h:80
std::shared_ptr< lldb_private::Target > TargetSP
std::shared_ptr< lldb_private::Module > ModuleSP
AllocationRecord(uintptr_t host_address, uint32_t permissions, lldb::SectionType sect_type, size_t size, unsigned alignment, unsigned section_id, const char *name)
JittedEntity(const char *name, lldb::addr_t local_addr=LLDB_INVALID_ADDRESS, lldb::addr_t remote_addr=LLDB_INVALID_ADDRESS)
Constructor.
ConstString m_name
The function's name.
lldb::addr_t m_remote_addr
The address of the function in the target's memory.
lldb::addr_t m_local_addr
The address of the function in LLDB's memory.
JittedFunction(const char *name, bool external, lldb::addr_t local_addr=LLDB_INVALID_ADDRESS, lldb::addr_t remote_addr=LLDB_INVALID_ADDRESS)
JittedGlobalVariable(const char *name, lldb::addr_t local_addr=LLDB_INVALID_ADDRESS, lldb::addr_t remote_addr=LLDB_INVALID_ADDRESS)