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;
77 ConstString("mmap"), eFunctionNameTypeFull, 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
132 allocated_addr =
133 call_plan_sp->GetReturnValueObject()->GetValueAsUnsigned(
135 if (process->GetAddressByteSize() == 4) {
136 if (allocated_addr == UINT32_MAX)
137 return false;
138 } else if (process->GetAddressByteSize() == 8) {
139 if (allocated_addr == UINT64_MAX)
140 return false;
141 }
142 return true;
143 }
144 }
145 }
146 }
147 }
148
149 return false;
150}
151
153 addr_t length) {
154 Thread *thread =
156 if (thread == nullptr)
157 return false;
158
159 ModuleFunctionSearchOptions function_options;
160 function_options.include_symbols = true;
161 function_options.include_inlines = false;
162
163 SymbolContextList sc_list;
164 process->GetTarget().GetImages().FindFunctions(
165 ConstString("munmap"), eFunctionNameTypeFull, function_options, sc_list);
166 const uint32_t count = sc_list.GetSize();
167 if (count > 0) {
169 options.SetStopOthers(true);
170 options.SetUnwindOnError(true);
171 options.SetIgnoreBreakpoints(true);
172 options.SetTryAllThreads(true);
173 options.SetDebug(false);
174 options.SetTimeout(process->GetUtilityExpressionTimeout());
175 options.SetTrapExceptions(false);
176
177 Address munmap_addr = GetCallableAddress(process->GetTarget(), sc_list);
178 if (munmap_addr.IsValid()) {
179 lldb::addr_t args[] = {addr, length};
181 *thread, munmap_addr, CompilerType(), args, options));
182 if (call_plan_sp) {
183 DiagnosticManager diagnostics;
184
185 StackFrame *frame = thread->GetStackFrameAtIndex(0).get();
186 if (frame) {
187 ExecutionContext exe_ctx;
188 frame->CalculateExecutionContext(exe_ctx);
189 ExpressionResults result = process->RunThreadPlan(
190 exe_ctx, call_plan_sp, options, diagnostics);
191 if (result == eExpressionCompleted) {
192 return true;
193 }
194 }
195 }
196 }
197 }
198
199 return false;
200}
#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.
A uniqued constant string class.
Definition ConstString.h:40
void SetUnwindOnError(bool unwind=false)
Definition Target.h:402
void SetTryAllThreads(bool try_others=true)
Definition Target.h:435
void SetTimeout(const Timeout< std::micro > &timeout)
Definition Target.h:423
void SetStopOthers(bool stop_others=true)
Definition Target.h:439
void SetIgnoreBreakpoints(bool ignore=false)
Definition Target.h:406
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
void FindFunctions(ConstString name, lldb::FunctionNameType name_type_mask, const ModuleFunctionSearchOptions &options, SymbolContextList &sc_list) const
std::chrono::seconds GetUtilityExpressionTimeout() const
Definition Process.cpp:361
A plug-in interface definition class for debugging a process.
Definition Process.h:367
ThreadList & GetThreadList()
Definition Process.h:2408
lldb::ExpressionResults RunThreadPlan(ExecutionContext &exe_ctx, lldb::ThreadPlanSP &thread_plan_sp, const EvaluateExpressionOptions &options, DiagnosticManager &diagnostic_manager)
Definition Process.cpp:5219
uint32_t GetAddressByteSize() const
Definition Process.cpp:3977
Target & GetTarget()
Get the target object pointer for this module.
Definition Process.h:1266
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:533
llvm::Expected< lldb::TypeSystemSP > GetScratchTypeSystemForLanguage(lldb::LanguageType language, bool create_on_demand=true)
Definition Target.cpp:2715
lldb::PlatformSP GetPlatform()
Definition Target.h:1980
const ModuleList & GetImages() const
Get accessor for the images for this process.
Definition Target.h:1254
const ArchSpec & GetArchitecture() const
Definition Target.h:1296
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:66
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::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:67
bool include_inlines
Include inlined functions.
Definition Module.h:71
bool include_symbols
Include the symbol table.
Definition Module.h:69