1 //===-- IRExecutionUnit.cpp -------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 // Other libraries and framework includes
13 #include "llvm/ExecutionEngine/ExecutionEngine.h"
14 #include "llvm/IR/LLVMContext.h"
15 #include "llvm/IR/Module.h"
16 #include "llvm/Support/SourceMgr.h"
18 #include "lldb/Core/DataBufferHeap.h"
19 #include "lldb/Core/DataExtractor.h"
20 #include "lldb/Core/Disassembler.h"
21 #include "lldb/Core/Log.h"
22 #include "lldb/Expression/IRExecutionUnit.h"
23 #include "lldb/Target/ExecutionContext.h"
24 #include "lldb/Target/Target.h"
26 using namespace lldb_private;
28 IRExecutionUnit::IRExecutionUnit (std::unique_ptr<llvm::LLVMContext> &context_ap,
29 std::unique_ptr<llvm::Module> &module_ap,
31 const lldb::TargetSP &target_sp,
32 std::vector<std::string> &cpu_features) :
33 IRMemoryMap(target_sp),
34 m_context_ap(context_ap.release()),
35 m_module_ap(module_ap.release()),
36 m_module(m_module_ap.get()),
37 m_cpu_features(cpu_features),
40 m_function_load_addr(LLDB_INVALID_ADDRESS),
41 m_function_end_load_addr(LLDB_INVALID_ADDRESS)
46 IRExecutionUnit::WriteNow (const uint8_t *bytes,
50 lldb::addr_t allocation_process_addr = Malloc (size,
52 lldb::ePermissionsWritable | lldb::ePermissionsReadable,
53 eAllocationPolicyMirror,
57 return LLDB_INVALID_ADDRESS;
59 WriteMemory(allocation_process_addr, bytes, size, error);
64 Free (allocation_process_addr, err);
66 return LLDB_INVALID_ADDRESS;
69 if (Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS))
71 DataBufferHeap my_buffer(size, 0);
73 ReadMemory(my_buffer.GetBytes(), allocation_process_addr, size, err);
77 DataExtractor my_extractor(my_buffer.GetBytes(), my_buffer.GetByteSize(), lldb::eByteOrderBig, 8);
78 my_extractor.PutToLog(log, 0, my_buffer.GetByteSize(), allocation_process_addr, 16, DataExtractor::TypeUInt8);
82 return allocation_process_addr;
86 IRExecutionUnit::FreeNow (lldb::addr_t allocation)
88 if (allocation == LLDB_INVALID_ADDRESS)
93 Free(allocation, err);
97 IRExecutionUnit::DisassembleFunction (Stream &stream,
98 lldb::ProcessSP &process_wp)
100 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
102 ExecutionContext exe_ctx(process_wp);
108 lldb::addr_t func_local_addr = LLDB_INVALID_ADDRESS;
109 lldb::addr_t func_remote_addr = LLDB_INVALID_ADDRESS;
111 for (JittedFunction &function : m_jitted_functions)
113 if (strstr(function.m_name.c_str(), m_name.AsCString()))
115 func_local_addr = function.m_local_addr;
116 func_remote_addr = function.m_remote_addr;
120 if (func_local_addr == LLDB_INVALID_ADDRESS)
122 ret.SetErrorToGenericError();
123 ret.SetErrorStringWithFormat("Couldn't find function %s for disassembly", m_name.AsCString());
128 log->Printf("Found function, has local address 0x%" PRIx64 " and remote address 0x%" PRIx64, (uint64_t)func_local_addr, (uint64_t)func_remote_addr);
130 std::pair <lldb::addr_t, lldb::addr_t> func_range;
132 func_range = GetRemoteRangeForLocal(func_local_addr);
134 if (func_range.first == 0 && func_range.second == 0)
136 ret.SetErrorToGenericError();
137 ret.SetErrorStringWithFormat("Couldn't find code range for function %s", m_name.AsCString());
142 log->Printf("Function's code range is [0x%" PRIx64 "+0x%" PRIx64 "]", func_range.first, func_range.second);
144 Target *target = exe_ctx.GetTargetPtr();
147 ret.SetErrorToGenericError();
148 ret.SetErrorString("Couldn't find the target");
152 lldb::DataBufferSP buffer_sp(new DataBufferHeap(func_range.second, 0));
154 Process *process = exe_ctx.GetProcessPtr();
156 process->ReadMemory(func_remote_addr, buffer_sp->GetBytes(), buffer_sp->GetByteSize(), err);
160 ret.SetErrorToGenericError();
161 ret.SetErrorStringWithFormat("Couldn't read from process: %s", err.AsCString("unknown error"));
165 ArchSpec arch(target->GetArchitecture());
167 const char *plugin_name = NULL;
168 const char *flavor_string = NULL;
169 lldb::DisassemblerSP disassembler_sp = Disassembler::FindPlugin(arch, flavor_string, plugin_name);
171 if (!disassembler_sp)
173 ret.SetErrorToGenericError();
174 ret.SetErrorStringWithFormat("Unable to find disassembler plug-in for %s architecture.", arch.GetArchitectureName());
180 ret.SetErrorToGenericError();
181 ret.SetErrorString("Couldn't find the process");
185 DataExtractor extractor(buffer_sp,
186 process->GetByteOrder(),
187 target->GetArchitecture().GetAddressByteSize());
191 log->Printf("Function data has contents:");
192 extractor.PutToLog (log,
194 extractor.GetByteSize(),
197 DataExtractor::TypeUInt8);
200 disassembler_sp->DecodeInstructions (Address (func_remote_addr), extractor, 0, UINT32_MAX, false, false);
202 InstructionList &instruction_list = disassembler_sp->GetInstructionList();
203 const uint32_t max_opcode_byte_size = instruction_list.GetMaxOpcocdeByteSize();
205 for (size_t instruction_index = 0, num_instructions = instruction_list.GetSize();
206 instruction_index < num_instructions;
209 Instruction *instruction = instruction_list.GetInstructionAtIndex(instruction_index).get();
210 instruction->Dump (&stream,
211 max_opcode_byte_size,
215 stream.PutChar('\n');
217 // FIXME: The DisassemblerLLVMC has a reference cycle and won't go away if it has any active instructions.
218 // I'll fix that but for now, just clear the list and it will go away nicely.
219 disassembler_sp->GetInstructionList().Clear();
223 static void ReportInlineAsmError(const llvm::SMDiagnostic &diagnostic, void *Context, unsigned LocCookie)
225 Error *err = static_cast<Error*>(Context);
227 if (err && err->Success())
229 err->SetErrorToGenericError();
230 err->SetErrorStringWithFormat("Inline assembly error: %s", diagnostic.getMessage().str().c_str());
235 IRExecutionUnit::GetRunnableInfo(Error &error,
236 lldb::addr_t &func_addr,
237 lldb::addr_t &func_end)
239 lldb::ProcessSP process_sp(GetProcessWP().lock());
241 static Mutex s_runnable_info_mutex(Mutex::Type::eMutexTypeRecursive);
243 func_addr = LLDB_INVALID_ADDRESS;
244 func_end = LLDB_INVALID_ADDRESS;
248 error.SetErrorToGenericError();
249 error.SetErrorString("Couldn't write the JIT compiled code into the process because the process is invalid");
255 func_addr = m_function_load_addr;
256 func_end = m_function_end_load_addr;
261 Mutex::Locker runnable_info_mutex_locker(s_runnable_info_mutex);
265 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
267 std::string error_string;
272 llvm::raw_string_ostream oss(s);
274 m_module->print(oss, NULL);
278 log->Printf ("Module being sent to JIT: \n%s", s.c_str());
281 llvm::Triple triple(m_module->getTargetTriple());
282 llvm::Function *function = m_module->getFunction (m_name.AsCString());
283 llvm::Reloc::Model relocModel;
284 llvm::CodeModel::Model codeModel;
286 if (triple.isOSBinFormatELF())
288 relocModel = llvm::Reloc::Static;
289 // This will be small for 32-bit and large for 64-bit.
290 codeModel = llvm::CodeModel::JITDefault;
294 relocModel = llvm::Reloc::PIC_;
295 codeModel = llvm::CodeModel::Small;
298 m_module_ap->getContext().setInlineAsmDiagnosticHandler(ReportInlineAsmError, &error);
300 llvm::EngineBuilder builder(m_module_ap.get());
302 builder.setEngineKind(llvm::EngineKind::JIT)
303 .setErrorStr(&error_string)
304 .setRelocationModel(relocModel)
305 .setJITMemoryManager(new MemoryManager(*this))
306 .setOptLevel(llvm::CodeGenOpt::Less)
307 .setAllocateGVsWithCode(true)
308 .setCodeModel(codeModel)
311 llvm::StringRef mArch;
312 llvm::StringRef mCPU;
313 llvm::SmallVector<std::string, 0> mAttrs;
315 for (std::string &feature : m_cpu_features)
316 mAttrs.push_back(feature);
318 llvm::TargetMachine *target_machine = builder.selectTarget(triple,
323 m_execution_engine_ap.reset(builder.create(target_machine));
325 if (!m_execution_engine_ap.get())
327 error.SetErrorToGenericError();
328 error.SetErrorStringWithFormat("Couldn't JIT the function: %s", error_string.c_str());
333 m_module_ap.release(); // ownership was transferred
336 m_execution_engine_ap->DisableLazyCompilation();
338 // We don't actually need the function pointer here, this just forces it to get resolved.
340 void *fun_ptr = m_execution_engine_ap->getPointerToFunction(function);
342 if (!error.Success())
344 // We got an error through our callback!
350 error.SetErrorToGenericError();
351 error.SetErrorStringWithFormat("Couldn't find '%s' in the JITted module", m_name.AsCString());
357 error.SetErrorToGenericError();
358 error.SetErrorStringWithFormat("'%s' was in the JITted module but wasn't lowered", m_name.AsCString());
362 m_jitted_functions.push_back (JittedFunction(m_name.AsCString(), (lldb::addr_t)fun_ptr));
364 CommitAllocations(process_sp);
365 ReportAllocations(*m_execution_engine_ap);
366 WriteData(process_sp);
368 for (JittedFunction &jitted_function : m_jitted_functions)
370 jitted_function.m_remote_addr = GetRemoteAddressForLocal (jitted_function.m_local_addr);
372 if (!jitted_function.m_name.compare(m_name.AsCString()))
374 AddrRange func_range = GetRemoteRangeForLocal(jitted_function.m_local_addr);
375 m_function_end_load_addr = func_range.first + func_range.second;
376 m_function_load_addr = jitted_function.m_remote_addr;
382 log->Printf("Code can be run in the target.");
384 StreamString disassembly_stream;
386 Error err = DisassembleFunction(disassembly_stream, process_sp);
390 log->Printf("Couldn't disassemble function : %s", err.AsCString("unknown error"));
394 log->Printf("Function disassembly:\n%s", disassembly_stream.GetData());
397 log->Printf("Sections: ");
398 for (AllocationRecord &record : m_records)
400 if (record.m_process_address != LLDB_INVALID_ADDRESS)
404 DataBufferHeap my_buffer(record.m_size, 0);
406 ReadMemory(my_buffer.GetBytes(), record.m_process_address, record.m_size, err);
410 DataExtractor my_extractor(my_buffer.GetBytes(), my_buffer.GetByteSize(), lldb::eByteOrderBig, 8);
411 my_extractor.PutToLog(log, 0, my_buffer.GetByteSize(), record.m_process_address, 16, DataExtractor::TypeUInt8);
417 func_addr = m_function_load_addr;
418 func_end = m_function_end_load_addr;
423 IRExecutionUnit::~IRExecutionUnit ()
426 m_execution_engine_ap.reset();
427 m_context_ap.reset();
430 IRExecutionUnit::MemoryManager::MemoryManager (IRExecutionUnit &parent) :
431 m_default_mm_ap (llvm::JITMemoryManager::CreateDefaultMemManager()),
437 IRExecutionUnit::MemoryManager::setMemoryWritable ()
439 m_default_mm_ap->setMemoryWritable();
443 IRExecutionUnit::MemoryManager::setMemoryExecutable ()
445 m_default_mm_ap->setMemoryExecutable();
450 IRExecutionUnit::MemoryManager::startFunctionBody(const llvm::Function *F,
451 uintptr_t &ActualSize)
453 return m_default_mm_ap->startFunctionBody(F, ActualSize);
457 IRExecutionUnit::MemoryManager::allocateStub(const llvm::GlobalValue* F,
461 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
463 uint8_t *return_value = m_default_mm_ap->allocateStub(F, StubSize, Alignment);
465 m_parent.m_records.push_back(AllocationRecord((uintptr_t)return_value,
466 lldb::ePermissionsReadable | lldb::ePermissionsWritable,
472 log->Printf("IRExecutionUnit::allocateStub (F=%p, StubSize=%u, Alignment=%u) = %p",
473 F, StubSize, Alignment, return_value);
480 IRExecutionUnit::MemoryManager::endFunctionBody(const llvm::Function *F,
481 uint8_t *FunctionStart,
482 uint8_t *FunctionEnd)
484 m_default_mm_ap->endFunctionBody(F, FunctionStart, FunctionEnd);
488 IRExecutionUnit::MemoryManager::allocateSpace(intptr_t Size, unsigned Alignment)
490 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
492 uint8_t *return_value = m_default_mm_ap->allocateSpace(Size, Alignment);
494 m_parent.m_records.push_back(AllocationRecord((uintptr_t)return_value,
495 lldb::ePermissionsReadable | lldb::ePermissionsWritable,
501 log->Printf("IRExecutionUnit::allocateSpace(Size=%" PRIu64 ", Alignment=%u) = %p",
502 (uint64_t)Size, Alignment, return_value);
509 IRExecutionUnit::MemoryManager::allocateCodeSection(uintptr_t Size,
512 llvm::StringRef SectionName)
514 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
516 uint8_t *return_value = m_default_mm_ap->allocateCodeSection(Size, Alignment, SectionID, SectionName);
518 m_parent.m_records.push_back(AllocationRecord((uintptr_t)return_value,
519 lldb::ePermissionsReadable | lldb::ePermissionsExecutable,
526 log->Printf("IRExecutionUnit::allocateCodeSection(Size=0x%" PRIx64 ", Alignment=%u, SectionID=%u) = %p",
527 (uint64_t)Size, Alignment, SectionID, return_value);
534 IRExecutionUnit::MemoryManager::allocateDataSection(uintptr_t Size,
537 llvm::StringRef SectionName,
540 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
542 uint8_t *return_value = m_default_mm_ap->allocateDataSection(Size, Alignment, SectionID, SectionName, IsReadOnly);
544 m_parent.m_records.push_back(AllocationRecord((uintptr_t)return_value,
545 lldb::ePermissionsReadable | lldb::ePermissionsWritable,
551 log->Printf("IRExecutionUnit::allocateDataSection(Size=0x%" PRIx64 ", Alignment=%u, SectionID=%u) = %p",
552 (uint64_t)Size, Alignment, SectionID, return_value);
559 IRExecutionUnit::MemoryManager::allocateGlobal(uintptr_t Size,
562 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
564 uint8_t *return_value = m_default_mm_ap->allocateGlobal(Size, Alignment);
566 m_parent.m_records.push_back(AllocationRecord((uintptr_t)return_value,
567 lldb::ePermissionsReadable | lldb::ePermissionsWritable,
573 log->Printf("IRExecutionUnit::allocateGlobal(Size=0x%" PRIx64 ", Alignment=%u) = %p",
574 (uint64_t)Size, Alignment, return_value);
581 IRExecutionUnit::MemoryManager::deallocateFunctionBody(void *Body)
583 m_default_mm_ap->deallocateFunctionBody(Body);
587 IRExecutionUnit::GetRemoteAddressForLocal (lldb::addr_t local_address)
589 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
591 for (AllocationRecord &record : m_records)
593 if (local_address >= record.m_host_address &&
594 local_address < record.m_host_address + record.m_size)
596 if (record.m_process_address == LLDB_INVALID_ADDRESS)
597 return LLDB_INVALID_ADDRESS;
599 lldb::addr_t ret = record.m_process_address + (local_address - record.m_host_address);
603 log->Printf("IRExecutionUnit::GetRemoteAddressForLocal() found 0x%" PRIx64 " in [0x%" PRIx64 "..0x%" PRIx64 "], and returned 0x%" PRIx64 " from [0x%" PRIx64 "..0x%" PRIx64 "].",
605 (uint64_t)record.m_host_address,
606 (uint64_t)record.m_host_address + (uint64_t)record.m_size,
608 record.m_process_address,
609 record.m_process_address + record.m_size);
616 return LLDB_INVALID_ADDRESS;
619 IRExecutionUnit::AddrRange
620 IRExecutionUnit::GetRemoteRangeForLocal (lldb::addr_t local_address)
622 for (AllocationRecord &record : m_records)
624 if (local_address >= record.m_host_address &&
625 local_address < record.m_host_address + record.m_size)
627 if (record.m_process_address == LLDB_INVALID_ADDRESS)
628 return AddrRange(0, 0);
630 return AddrRange(record.m_process_address, record.m_size);
634 return AddrRange (0, 0);
638 IRExecutionUnit::CommitAllocations (lldb::ProcessSP &process_sp)
642 lldb_private::Error err;
644 for (AllocationRecord &record : m_records)
646 if (record.m_process_address != LLDB_INVALID_ADDRESS)
650 record.m_process_address = Malloc(record.m_size,
652 record.m_permissions,
653 eAllocationPolicyProcessOnly,
665 for (AllocationRecord &record : m_records)
667 if (record.m_process_address != LLDB_INVALID_ADDRESS)
669 Free(record.m_process_address, err);
670 record.m_process_address = LLDB_INVALID_ADDRESS;
679 IRExecutionUnit::ReportAllocations (llvm::ExecutionEngine &engine)
681 for (AllocationRecord &record : m_records)
683 if (record.m_process_address == LLDB_INVALID_ADDRESS)
686 if (record.m_section_id == eSectionIDInvalid)
689 engine.mapSectionAddress((void*)record.m_host_address, record.m_process_address);
692 // Trigger re-application of relocations.
693 engine.finalizeObject();
697 IRExecutionUnit::WriteData (lldb::ProcessSP &process_sp)
699 for (AllocationRecord &record : m_records)
701 if (record.m_process_address == LLDB_INVALID_ADDRESS)
704 lldb_private::Error err;
706 WriteMemory (record.m_process_address, (uint8_t*)record.m_host_address, record.m_size, err);
713 IRExecutionUnit::AllocationRecord::dump (Log *log)
718 log->Printf("[0x%llx+0x%llx]->0x%llx (alignment %d, section ID %d)",
719 (unsigned long long)m_host_address,
720 (unsigned long long)m_size,
721 (unsigned long long)m_process_address,
722 (unsigned)m_alignment,
723 (unsigned)m_section_id);