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"
14#include "lldb/Host/Config.h"
19#include "lldb/Target/Process.h"
20#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
36bool lldb_private::InferiorCallMmap(Process *process, addr_t &allocated_addr,
37 addr_t addr, addr_t length, unsigned prot,
38 unsigned flags, addr_t fd, addr_t offset) {
39 Thread *thread =
41 if (thread == nullptr)
42 return false;
43
44 ModuleFunctionSearchOptions function_options;
45 function_options.include_symbols = true;
46 function_options.include_inlines = false;
47
48 SymbolContextList sc_list;
50 ConstString("mmap"), eFunctionNameTypeFull, function_options, sc_list);
51 const uint32_t count = sc_list.GetSize();
52 if (count > 0) {
54 if (sc_list.GetContextAtIndex(0, sc)) {
55 const uint32_t range_scope =
56 eSymbolContextFunction | eSymbolContextSymbol;
57 const bool use_inline_block_range = false;
59 options.SetStopOthers(true);
60 options.SetUnwindOnError(true);
61 options.SetIgnoreBreakpoints(true);
62 options.SetTryAllThreads(true);
63 options.SetDebug(false);
64 options.SetTimeout(process->GetUtilityExpressionTimeout());
65 options.SetTrapExceptions(false);
66
67 addr_t prot_arg;
68 if (prot == eMmapProtNone)
69 prot_arg = PROT_NONE;
70 else {
71 prot_arg = 0;
72 if (prot & eMmapProtExec)
73 prot_arg |= PROT_EXEC;
74 if (prot & eMmapProtRead)
75 prot_arg |= PROT_READ;
76 if (prot & eMmapProtWrite)
77 prot_arg |= PROT_WRITE;
78 }
79
80 AddressRange mmap_range;
81 if (sc.GetAddressRange(range_scope, 0, use_inline_block_range,
82 mmap_range)) {
83 auto type_system_or_err =
86 if (!type_system_or_err) {
87 llvm::consumeError(type_system_or_err.takeError());
88 return false;
89 }
90 auto ts = *type_system_or_err;
91 if (!ts)
92 return false;
93 CompilerType void_ptr_type =
95 const ArchSpec arch = process->GetTarget().GetArchitecture();
96 MmapArgList args =
97 process->GetTarget().GetPlatform()->GetMmapArgumentList(
98 arch, addr, length, prot_arg, flags, fd, offset);
99 lldb::ThreadPlanSP call_plan_sp(
100 new ThreadPlanCallFunction(*thread, mmap_range.GetBaseAddress(),
101 void_ptr_type, args, options));
102 if (call_plan_sp) {
103 DiagnosticManager diagnostics;
104
105 StackFrame *frame = thread->GetStackFrameAtIndex(0).get();
106 if (frame) {
107 ExecutionContext exe_ctx;
108 frame->CalculateExecutionContext(exe_ctx);
109 ExpressionResults result = process->RunThreadPlan(
110 exe_ctx, call_plan_sp, options, diagnostics);
111 if (result == eExpressionCompleted) {
112
113 allocated_addr =
114 call_plan_sp->GetReturnValueObject()->GetValueAsUnsigned(
116 if (process->GetAddressByteSize() == 4) {
117 if (allocated_addr == UINT32_MAX)
118 return false;
119 } else if (process->GetAddressByteSize() == 8) {
120 if (allocated_addr == UINT64_MAX)
121 return false;
122 }
123 return true;
124 }
125 }
126 }
127 }
128 }
129 }
130
131 return false;
132}
133
135 addr_t length) {
136 Thread *thread =
138 if (thread == nullptr)
139 return false;
140
141 ModuleFunctionSearchOptions function_options;
142 function_options.include_symbols = true;
143 function_options.include_inlines = false;
144
145 SymbolContextList sc_list;
146 process->GetTarget().GetImages().FindFunctions(
147 ConstString("munmap"), eFunctionNameTypeFull, function_options, sc_list);
148 const uint32_t count = sc_list.GetSize();
149 if (count > 0) {
150 SymbolContext sc;
151 if (sc_list.GetContextAtIndex(0, sc)) {
152 const uint32_t range_scope =
153 eSymbolContextFunction | eSymbolContextSymbol;
154 const bool use_inline_block_range = false;
156 options.SetStopOthers(true);
157 options.SetUnwindOnError(true);
158 options.SetIgnoreBreakpoints(true);
159 options.SetTryAllThreads(true);
160 options.SetDebug(false);
161 options.SetTimeout(process->GetUtilityExpressionTimeout());
162 options.SetTrapExceptions(false);
163
164 AddressRange munmap_range;
165 if (sc.GetAddressRange(range_scope, 0, use_inline_block_range,
166 munmap_range)) {
167 lldb::addr_t args[] = {addr, length};
168 lldb::ThreadPlanSP call_plan_sp(
169 new ThreadPlanCallFunction(*thread, munmap_range.GetBaseAddress(),
170 CompilerType(), args, options));
171 if (call_plan_sp) {
172 DiagnosticManager diagnostics;
173
174 StackFrame *frame = thread->GetStackFrameAtIndex(0).get();
175 if (frame) {
176 ExecutionContext exe_ctx;
177 frame->CalculateExecutionContext(exe_ctx);
178 ExpressionResults result = process->RunThreadPlan(
179 exe_ctx, call_plan_sp, options, diagnostics);
180 if (result == eExpressionCompleted) {
181 return true;
182 }
183 }
184 }
185 }
186 }
187 }
188
189 return false;
190}
#define PROT_READ
#define PROT_WRITE
#define PROT_NONE
#define PROT_EXEC
A section + offset based address range class.
Definition: AddressRange.h:25
Address & GetBaseAddress()
Get accessor for the base address of the range.
Definition: AddressRange.h:209
An architecture specification class.
Definition: ArchSpec.h:31
Generic representation of a type in a programming language.
Definition: CompilerType.h:36
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:334
void SetTryAllThreads(bool try_others=true)
Definition: Target.h:367
void SetTimeout(const Timeout< std::micro > &timeout)
Definition: Target.h:355
void SetStopOthers(bool stop_others=true)
Definition: Target.h:371
void SetIgnoreBreakpoints(bool ignore=false)
Definition: Target.h:338
"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:319
A plug-in interface definition class for debugging a process.
Definition: Process.h:341
ThreadList & GetThreadList()
Definition: Process.h:2213
lldb::ExpressionResults RunThreadPlan(ExecutionContext &exe_ctx, lldb::ThreadPlanSP &thread_plan_sp, const EvaluateExpressionOptions &options, DiagnosticManager &diagnostic_manager)
Definition: Process.cpp:4731
uint32_t GetAddressByteSize() const
Definition: Process.cpp:3404
Target & GetTarget()
Get the target object pointer for this module.
Definition: Process.h:1277
This base class provides an interface to stack frames.
Definition: StackFrame.h:42
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.
Definition: SymbolContext.h:34
bool GetAddressRange(uint32_t scope, uint32_t range_idx, bool use_inline_block_range, AddressRange &range) const
Get the address range contained within a symbol context.
llvm::Expected< lldb::TypeSystemSP > GetScratchTypeSystemForLanguage(lldb::LanguageType language, bool create_on_demand=true)
Definition: Target.cpp:2414
lldb::PlatformSP GetPlatform()
Definition: Target.h:1433
const ModuleList & GetImages() const
Get accessor for the images for this process.
Definition: Target.h:972
const ArchSpec & GetArchitecture() const
Definition: Target.h:1014
lldb::ThreadSP GetExpressionExecutionThread()
Definition: ThreadList.cpp:60
virtual lldb::StackFrameSP GetStackFrameAtIndex(uint32_t idx)
Definition: Thread.h:405
#define UINT64_MAX
Definition: lldb-defines.h:23
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:82
#define UINT32_MAX
Definition: lldb-defines.h:19
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14
bool InferiorCallMunmap(Process *proc, lldb::addr_t addr, lldb::addr_t length)
llvm::SmallVector< lldb::addr_t, 6 > MmapArgList
Definition: Platform.h:61
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)
Definition: SBAddress.h:15
std::shared_ptr< lldb_private::ThreadPlan > ThreadPlanSP
Definition: lldb-forward.h:441
@ eLanguageTypeC
Non-standardized C, such as K&R.
ExpressionResults
The results of expression evaluation.
@ eExpressionCompleted
uint64_t addr_t
Definition: lldb-types.h:79
Options used by Module::FindFunctions.
Definition: Module.h:65
bool include_inlines
Include inlined functions.
Definition: Module.h:69
bool include_symbols
Include the symbol table.
Definition: Module.h:67