Path: blob/main/contrib/llvm-project/lldb/source/Plugins/Process/Utility/InferiorCallPOSIX.cpp
39644 views
//===-- InferiorCallPOSIX.cpp ---------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "InferiorCallPOSIX.h"9#include "lldb/Core/Address.h"10#include "lldb/Core/Module.h"11#include "lldb/Core/ValueObject.h"12#include "lldb/Expression/DiagnosticManager.h"13#include "lldb/Host/Config.h"14#include "lldb/Symbol/SymbolContext.h"15#include "lldb/Symbol/TypeSystem.h"16#include "lldb/Target/ExecutionContext.h"17#include "lldb/Target/Platform.h"18#include "lldb/Target/Process.h"19#include "lldb/Target/Target.h"20#include "lldb/Target/ThreadPlanCallFunction.h"2122#if LLDB_ENABLE_POSIX23#include <sys/mman.h>24#else25// define them26#define PROT_NONE 027#define PROT_READ 128#define PROT_WRITE 229#define PROT_EXEC 430#endif3132using namespace lldb;33using namespace lldb_private;3435bool lldb_private::InferiorCallMmap(Process *process, addr_t &allocated_addr,36addr_t addr, addr_t length, unsigned prot,37unsigned flags, addr_t fd, addr_t offset) {38Thread *thread =39process->GetThreadList().GetExpressionExecutionThread().get();40if (thread == nullptr)41return false;4243ModuleFunctionSearchOptions function_options;44function_options.include_symbols = true;45function_options.include_inlines = false;4647SymbolContextList sc_list;48process->GetTarget().GetImages().FindFunctions(49ConstString("mmap"), eFunctionNameTypeFull, function_options, sc_list);50const uint32_t count = sc_list.GetSize();51if (count > 0) {52SymbolContext sc;53if (sc_list.GetContextAtIndex(0, sc)) {54const uint32_t range_scope =55eSymbolContextFunction | eSymbolContextSymbol;56const bool use_inline_block_range = false;57EvaluateExpressionOptions options;58options.SetStopOthers(true);59options.SetUnwindOnError(true);60options.SetIgnoreBreakpoints(true);61options.SetTryAllThreads(true);62options.SetDebug(false);63options.SetTimeout(process->GetUtilityExpressionTimeout());64options.SetTrapExceptions(false);6566addr_t prot_arg;67if (prot == eMmapProtNone)68prot_arg = PROT_NONE;69else {70prot_arg = 0;71if (prot & eMmapProtExec)72prot_arg |= PROT_EXEC;73if (prot & eMmapProtRead)74prot_arg |= PROT_READ;75if (prot & eMmapProtWrite)76prot_arg |= PROT_WRITE;77}7879AddressRange mmap_range;80if (sc.GetAddressRange(range_scope, 0, use_inline_block_range,81mmap_range)) {82auto type_system_or_err =83process->GetTarget().GetScratchTypeSystemForLanguage(84eLanguageTypeC);85if (!type_system_or_err) {86llvm::consumeError(type_system_or_err.takeError());87return false;88}89auto ts = *type_system_or_err;90if (!ts)91return false;92CompilerType void_ptr_type =93ts->GetBasicTypeFromAST(eBasicTypeVoid).GetPointerType();94const ArchSpec arch = process->GetTarget().GetArchitecture();95MmapArgList args =96process->GetTarget().GetPlatform()->GetMmapArgumentList(97arch, addr, length, prot_arg, flags, fd, offset);98lldb::ThreadPlanSP call_plan_sp(99new ThreadPlanCallFunction(*thread, mmap_range.GetBaseAddress(),100void_ptr_type, args, options));101if (call_plan_sp) {102DiagnosticManager diagnostics;103104StackFrame *frame = thread->GetStackFrameAtIndex(0).get();105if (frame) {106ExecutionContext exe_ctx;107frame->CalculateExecutionContext(exe_ctx);108ExpressionResults result = process->RunThreadPlan(109exe_ctx, call_plan_sp, options, diagnostics);110if (result == eExpressionCompleted) {111112allocated_addr =113call_plan_sp->GetReturnValueObject()->GetValueAsUnsigned(114LLDB_INVALID_ADDRESS);115if (process->GetAddressByteSize() == 4) {116if (allocated_addr == UINT32_MAX)117return false;118} else if (process->GetAddressByteSize() == 8) {119if (allocated_addr == UINT64_MAX)120return false;121}122return true;123}124}125}126}127}128}129130return false;131}132133bool lldb_private::InferiorCallMunmap(Process *process, addr_t addr,134addr_t length) {135Thread *thread =136process->GetThreadList().GetExpressionExecutionThread().get();137if (thread == nullptr)138return false;139140ModuleFunctionSearchOptions function_options;141function_options.include_symbols = true;142function_options.include_inlines = false;143144SymbolContextList sc_list;145process->GetTarget().GetImages().FindFunctions(146ConstString("munmap"), eFunctionNameTypeFull, function_options, sc_list);147const uint32_t count = sc_list.GetSize();148if (count > 0) {149SymbolContext sc;150if (sc_list.GetContextAtIndex(0, sc)) {151const uint32_t range_scope =152eSymbolContextFunction | eSymbolContextSymbol;153const bool use_inline_block_range = false;154EvaluateExpressionOptions options;155options.SetStopOthers(true);156options.SetUnwindOnError(true);157options.SetIgnoreBreakpoints(true);158options.SetTryAllThreads(true);159options.SetDebug(false);160options.SetTimeout(process->GetUtilityExpressionTimeout());161options.SetTrapExceptions(false);162163AddressRange munmap_range;164if (sc.GetAddressRange(range_scope, 0, use_inline_block_range,165munmap_range)) {166lldb::addr_t args[] = {addr, length};167lldb::ThreadPlanSP call_plan_sp(168new ThreadPlanCallFunction(*thread, munmap_range.GetBaseAddress(),169CompilerType(), args, options));170if (call_plan_sp) {171DiagnosticManager diagnostics;172173StackFrame *frame = thread->GetStackFrameAtIndex(0).get();174if (frame) {175ExecutionContext exe_ctx;176frame->CalculateExecutionContext(exe_ctx);177ExpressionResults result = process->RunThreadPlan(178exe_ctx, call_plan_sp, options, diagnostics);179if (result == eExpressionCompleted) {180return true;181}182}183}184}185}186}187188return false;189}190191192