LLDB mainline
InferiorCallPOSIX.cpp
Go to the documentation of this file.
1//===-- InferiorCallPOSIX.cpp ---------------------------------------------===//
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#include "InferiorCallPOSIX.h"
10#include "lldb/Core/Address.h"
11#include "lldb/Core/Module.h"
13#include "lldb/Host/Config.h"
18#include "lldb/Target/Process.h"
19#include "lldb/Target/Target.h"
22
23#if LLDB_ENABLE_POSIX
24#include <sys/mman.h>
25#else
26// define them
27#define PROT_NONE 0
28#define PROT_READ 1
29#define PROT_WRITE 2
30#define PROT_EXEC 4
31#endif
32
33using namespace lldb;
34using namespace lldb_private;
35
36/// Pick a call target from \p sc_list. A symbol from an ELF filter/auxiliary
37/// library is resolved through its filtee(s) first, falling back to its own
38/// definition only if none of them provide it, mirroring what the dynamic
39/// linker does.
41 const SymbolContextList &sc_list) {
42 const uint32_t count = sc_list.GetSize();
43 for (uint32_t i = 0; i < count; ++i) {
45 if (!sc_list.GetContextAtIndex(i, sc))
46 continue;
47 if (sc.symbol) {
48 // Pass the containing module so all of its re-exported libraries
49 // (e.g. an ELF filter library's filtees) are searched in order.
50 if (Symbol *actual =
52 return actual->GetAddress();
53 // A pure re-export placeholder with no filtee providing a definition
54 // has no code of its own and isn't callable.
56 continue;
57 }
59 }
60 return Address();
61}
62
63bool lldb_private::InferiorCallMmap(Process *process, addr_t &allocated_addr,
64 addr_t addr, addr_t length, unsigned prot,
65 unsigned flags, addr_t fd, addr_t offset) {
66 Thread *thread =
68 if (thread == nullptr)
69 return false;
70
71 ModuleFunctionSearchOptions function_options;
72 function_options.include_symbols = true;
73 function_options.include_inlines = false;
74
75 SymbolContextList sc_list;
76 process->GetTarget().GetImages().FindFunctions("mmap", eFunctionNameTypeFull,
77 function_options, sc_list);
78 const uint32_t count = sc_list.GetSize();
79 if (count > 0) {
81 options.SetStopOthers(true);
82 options.SetUnwindOnError(true);
83 options.SetIgnoreBreakpoints(true);
84 options.SetTryAllThreads(true);
85 options.SetDebug(false);
86 options.SetTimeout(process->GetUtilityExpressionTimeout());
87 options.SetTrapExceptions(false);
88
89 addr_t prot_arg;
90 if (prot == eMmapProtNone)
91 prot_arg = PROT_NONE;
92 else {
93 prot_arg = 0;
94 if (prot & eMmapProtExec)
95 prot_arg |= PROT_EXEC;
96 if (prot & eMmapProtRead)
97 prot_arg |= PROT_READ;
98 if (prot & eMmapProtWrite)
99 prot_arg |= PROT_WRITE;
100 }
101
102 Address mmap_addr = GetCallableAddress(process->GetTarget(), sc_list);
103 if (mmap_addr.IsValid()) {
104 auto type_system_or_err =
106 if (!type_system_or_err) {
107 llvm::consumeError(type_system_or_err.takeError());
108 return false;
109 }
110 auto ts = *type_system_or_err;
111 if (!ts)
112 return false;
113 CompilerType void_ptr_type =
115 const ArchSpec arch = process->GetTarget().GetArchitecture();
116 MmapArgList args =
117 process->GetTarget().GetPlatform()->GetMmapArgumentList(
118 arch, addr, length, prot_arg, flags, fd, offset);
120 *thread, mmap_addr, void_ptr_type, args, options));
121 if (call_plan_sp) {
122 DiagnosticManager diagnostics;
123
124 StackFrame *frame = thread->GetStackFrameAtIndex(0).get();
125 if (frame) {
126 ExecutionContext exe_ctx;
127 frame->CalculateExecutionContext(exe_ctx);
128 ExpressionResults result = process->RunThreadPlan(
129 exe_ctx, call_plan_sp, options, diagnostics);
130 if (result == eExpressionCompleted) {
131 ThreadPlanReturnValue return_value = call_plan_sp->GetReturnValue();
132 auto *returned = std::get_if<ReturnedValue>(&return_value);
133 if (!returned)
134 return false;
135
136 allocated_addr =
137 returned->valobj_sp->GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
138 if (process->GetAddressByteSize() == 4) {
139 if (allocated_addr == UINT32_MAX)
140 return false;
141 } else if (process->GetAddressByteSize() == 8) {
142 if (allocated_addr == UINT64_MAX)
143 return false;
144 }
145 return true;
146 }
147 }
148 }
149 }
150 }
151
152 return false;
153}
154
156 addr_t length) {
157 Thread *thread =
159 if (thread == nullptr)
160 return false;
161
162 ModuleFunctionSearchOptions function_options;
163 function_options.include_symbols = true;
164 function_options.include_inlines = false;
165
166 SymbolContextList sc_list;
167 process->GetTarget().GetImages().FindFunctions(
168 "munmap", eFunctionNameTypeFull, function_options, sc_list);
169 const uint32_t count = sc_list.GetSize();
170 if (count > 0) {
172 options.SetStopOthers(true);
173 options.SetUnwindOnError(true);
174 options.SetIgnoreBreakpoints(true);
175 options.SetTryAllThreads(true);
176 options.SetDebug(false);
177 options.SetTimeout(process->GetUtilityExpressionTimeout());
178 options.SetTrapExceptions(false);
179
180 Address munmap_addr = GetCallableAddress(process->GetTarget(), sc_list);
181 if (munmap_addr.IsValid()) {
182 lldb::addr_t args[] = {addr, length};
184 *thread, munmap_addr, CompilerType(), args, options));
185 if (call_plan_sp) {
186 DiagnosticManager diagnostics;
187
188 StackFrame *frame = thread->GetStackFrameAtIndex(0).get();
189 if (frame) {
190 ExecutionContext exe_ctx;
191 frame->CalculateExecutionContext(exe_ctx);
192 ExpressionResults result = process->RunThreadPlan(
193 exe_ctx, call_plan_sp, options, diagnostics);
194 if (result == eExpressionCompleted) {
195 return true;
196 }
197 }
198 }
199 }
200 }
201
202 return false;
203}
#define PROT_READ
#define PROT_WRITE
#define PROT_NONE
#define PROT_EXEC
static Address GetCallableAddress(Target &target, const SymbolContextList &sc_list)
Pick a call target from sc_list.
A section + offset based address class.
Definition Address.h:62
bool IsValid() const
Check if the object state is valid.
Definition Address.h:355
An architecture specification class.
Definition ArchSpec.h:32
Generic representation of a type in a programming language.
CompilerType GetBasicTypeFromAST(lldb::BasicType basic_type) const
Create related types using the current type's AST.
CompilerType GetPointerType() const
Return a new CompilerType that is a pointer to this type.
void SetUnwindOnError(bool unwind=false)
Definition Target.h:412
void SetTryAllThreads(bool try_others=true)
Definition Target.h:445
void SetTimeout(const Timeout< std::micro > &timeout)
Definition Target.h:433
void SetStopOthers(bool stop_others=true)
Definition Target.h:449
void SetIgnoreBreakpoints(bool ignore=false)
Definition Target.h:416
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
void FindFunctions(llvm::StringRef name, lldb::FunctionNameType name_type_mask, const ModuleFunctionSearchOptions &options, SymbolContextList &sc_list) const
std::chrono::seconds GetUtilityExpressionTimeout() const
Definition Process.cpp:381
A plug-in interface definition class for debugging a process.
Definition Process.h:370
ThreadList & GetThreadList()
Definition Process.h:2398
lldb::ExpressionResults RunThreadPlan(ExecutionContext &exe_ctx, lldb::ThreadPlanSP &thread_plan_sp, const EvaluateExpressionOptions &requested_options, DiagnosticManager &diagnostic_manager)
Definition Process.cpp:5390
uint32_t GetAddressByteSize() const
Definition Process.cpp:4094
Target & GetTarget()
Get the target object pointer for this module.
Definition Process.h:1271
This base class provides an interface to stack frames.
Definition StackFrame.h:44
void CalculateExecutionContext(ExecutionContext &exe_ctx) override
Reconstruct the object's execution context into sc.
Defines a list of symbol context objects.
bool GetContextAtIndex(size_t idx, SymbolContext &sc) const
Get accessor for a symbol context at index idx.
uint32_t GetSize() const
Get accessor for a symbol context list size.
Defines a symbol context baton that can be handed other debug core functions.
lldb::ModuleSP module_sp
The Module for a given query.
Symbol * symbol
The Symbol for a given query.
Address GetFunctionOrSymbolAddress() const
Get the address of the function or symbol represented by this symbol context.
lldb::SymbolType GetType() const
Definition Symbol.h:197
Symbol * ResolveReExportedSymbol(Target &target, const lldb::ModuleSP &containing_module_sp=lldb::ModuleSP()) const
Find the symbol this re-exported symbol resolves to.
Definition Symbol.cpp:536
llvm::Expected< lldb::TypeSystemSP > GetScratchTypeSystemForLanguage(lldb::LanguageType language, bool create_on_demand=true)
Definition Target.cpp:2793
lldb::PlatformSP GetPlatform()
Definition Target.h:2053
const ModuleList & GetImages() const
Get accessor for the images for this process.
Definition Target.h:1295
const ArchSpec & GetArchitecture() const
Definition Target.h:1337
lldb::ThreadSP GetExpressionExecutionThread()
#define UINT64_MAX
#define LLDB_INVALID_ADDRESS
#define UINT32_MAX
A class that represents a running process on the host machine.
bool InferiorCallMunmap(Process *proc, lldb::addr_t addr, lldb::addr_t length)
llvm::SmallVector< lldb::addr_t, 6 > MmapArgList
Definition Platform.h:68
bool InferiorCallMmap(Process *proc, lldb::addr_t &allocated_addr, lldb::addr_t addr, lldb::addr_t length, unsigned prot, unsigned flags, lldb::addr_t fd, lldb::addr_t offset)
std::variant< std::monostate, ReturnedValue, ThrownError > ThreadPlanReturnValue
What a completed thread plan computed for the function it stepped out of or called: nothing,...
Definition Thread.h:82
std::shared_ptr< lldb_private::ThreadPlan > ThreadPlanSP
@ eLanguageTypeC
Non-standardized C, such as K&R.
ExpressionResults
The results of expression evaluation.
@ eExpressionCompleted
@ eSymbolTypeReExported
uint64_t addr_t
Definition lldb-types.h:80
Options used by Module::FindFunctions.
Definition Module.h:68
bool include_inlines
Include inlined functions.
Definition Module.h:72
bool include_symbols
Include the symbol table.
Definition Module.h:70