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"
12 #include "lldb/Core/StreamFile.h"
13 #include "lldb/Core/ValueObject.h"
15 #include "lldb/Host/Config.h"
17 #include "lldb/Symbol/TypeSystem.h"
19 #include "lldb/Target/Platform.h"
20 #include "lldb/Target/Process.h"
21 #include "lldb/Target/Target.h"
23 
24 #if LLDB_ENABLE_POSIX
25 #include <sys/mman.h>
26 #else
27 // define them
28 #define PROT_NONE 0
29 #define PROT_READ 1
30 #define PROT_WRITE 2
31 #define PROT_EXEC 4
32 #endif
33 
34 using namespace lldb;
35 using namespace lldb_private;
36 
37 bool lldb_private::InferiorCallMmap(Process *process, addr_t &allocated_addr,
38  addr_t addr, addr_t length, unsigned prot,
39  unsigned flags, addr_t fd, addr_t offset) {
40  Thread *thread =
42  if (thread == nullptr)
43  return false;
44 
45  ModuleFunctionSearchOptions function_options;
46  function_options.include_symbols = true;
47  function_options.include_inlines = false;
48 
49  SymbolContextList sc_list;
50  process->GetTarget().GetImages().FindFunctions(
51  ConstString("mmap"), eFunctionNameTypeFull, function_options, sc_list);
52  const uint32_t count = sc_list.GetSize();
53  if (count > 0) {
54  SymbolContext sc;
55  if (sc_list.GetContextAtIndex(0, sc)) {
56  const uint32_t range_scope =
57  eSymbolContextFunction | eSymbolContextSymbol;
58  const bool use_inline_block_range = false;
60  options.SetStopOthers(true);
61  options.SetUnwindOnError(true);
62  options.SetIgnoreBreakpoints(true);
63  options.SetTryAllThreads(true);
64  options.SetDebug(false);
65  options.SetTimeout(process->GetUtilityExpressionTimeout());
66  options.SetTrapExceptions(false);
67 
68  addr_t prot_arg;
69  if (prot == eMmapProtNone)
70  prot_arg = PROT_NONE;
71  else {
72  prot_arg = 0;
73  if (prot & eMmapProtExec)
74  prot_arg |= PROT_EXEC;
75  if (prot & eMmapProtRead)
76  prot_arg |= PROT_READ;
77  if (prot & eMmapProtWrite)
78  prot_arg |= PROT_WRITE;
79  }
80 
81  AddressRange mmap_range;
82  if (sc.GetAddressRange(range_scope, 0, use_inline_block_range,
83  mmap_range)) {
84  auto type_system_or_err =
87  if (!type_system_or_err) {
88  llvm::consumeError(type_system_or_err.takeError());
89  return false;
90  }
91  CompilerType void_ptr_type =
92  type_system_or_err->GetBasicTypeFromAST(eBasicTypeVoid)
93  .GetPointerType();
94  const ArchSpec arch = process->GetTarget().GetArchitecture();
95  MmapArgList args =
96  process->GetTarget().GetPlatform()->GetMmapArgumentList(
97  arch, addr, length, prot_arg, flags, fd, offset);
98  lldb::ThreadPlanSP call_plan_sp(
99  new ThreadPlanCallFunction(*thread, mmap_range.GetBaseAddress(),
100  void_ptr_type, args, options));
101  if (call_plan_sp) {
102  DiagnosticManager diagnostics;
103 
104  StackFrame *frame = thread->GetStackFrameAtIndex(0).get();
105  if (frame) {
106  ExecutionContext exe_ctx;
107  frame->CalculateExecutionContext(exe_ctx);
108  ExpressionResults result = process->RunThreadPlan(
109  exe_ctx, call_plan_sp, options, diagnostics);
110  if (result == eExpressionCompleted) {
111 
112  allocated_addr =
113  call_plan_sp->GetReturnValueObject()->GetValueAsUnsigned(
115  if (process->GetAddressByteSize() == 4) {
116  if (allocated_addr == UINT32_MAX)
117  return false;
118  } else if (process->GetAddressByteSize() == 8) {
119  if (allocated_addr == UINT64_MAX)
120  return false;
121  }
122  return true;
123  }
124  }
125  }
126  }
127  }
128  }
129 
130  return false;
131 }
132 
134  addr_t length) {
135  Thread *thread =
136  process->GetThreadList().GetExpressionExecutionThread().get();
137  if (thread == nullptr)
138  return false;
139 
140  ModuleFunctionSearchOptions function_options;
141  function_options.include_symbols = true;
142  function_options.include_inlines = false;
143 
144  SymbolContextList sc_list;
145  process->GetTarget().GetImages().FindFunctions(
146  ConstString("munmap"), eFunctionNameTypeFull, function_options, sc_list);
147  const uint32_t count = sc_list.GetSize();
148  if (count > 0) {
149  SymbolContext sc;
150  if (sc_list.GetContextAtIndex(0, sc)) {
151  const uint32_t range_scope =
152  eSymbolContextFunction | eSymbolContextSymbol;
153  const bool use_inline_block_range = false;
155  options.SetStopOthers(true);
156  options.SetUnwindOnError(true);
157  options.SetIgnoreBreakpoints(true);
158  options.SetTryAllThreads(true);
159  options.SetDebug(false);
160  options.SetTimeout(process->GetUtilityExpressionTimeout());
161  options.SetTrapExceptions(false);
162 
163  AddressRange munmap_range;
164  if (sc.GetAddressRange(range_scope, 0, use_inline_block_range,
165  munmap_range)) {
166  lldb::addr_t args[] = {addr, length};
167  lldb::ThreadPlanSP call_plan_sp(
168  new ThreadPlanCallFunction(*thread, munmap_range.GetBaseAddress(),
169  CompilerType(), args, options));
170  if (call_plan_sp) {
171  DiagnosticManager diagnostics;
172 
173  StackFrame *frame = thread->GetStackFrameAtIndex(0).get();
174  if (frame) {
175  ExecutionContext exe_ctx;
176  frame->CalculateExecutionContext(exe_ctx);
177  ExpressionResults result = process->RunThreadPlan(
178  exe_ctx, call_plan_sp, options, diagnostics);
179  if (result == eExpressionCompleted) {
180  return true;
181  }
182  }
183  }
184  }
185  }
186  }
187 
188  return false;
189 }
lldb_private::AddressRange::GetBaseAddress
Address & GetBaseAddress()
Get accessor for the base address of the range.
Definition: AddressRange.h:209
InferiorCallPOSIX.h
lldb_private::ExecutionContext
Definition: ExecutionContext.h:292
lldb_private::ArchSpec
Definition: ArchSpec.h:33
lldb_private::MmapArgList
llvm::SmallVector< lldb::addr_t, 6 > MmapArgList
Definition: Platform.h:58
lldb_private::EvaluateExpressionOptions::SetTimeout
void SetTimeout(const Timeout< std::micro > &timeout)
Definition: Target.h:326
lldb_private::SymbolContext::GetAddressRange
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.
Definition: SymbolContext.cpp:379
lldb::ExpressionResults
ExpressionResults
The results of expression evaluation.
Definition: lldb-enumerations.h:270
lldb_private::ProcessProperties::GetUtilityExpressionTimeout
std::chrono::seconds GetUtilityExpressionTimeout() const
Definition: Process.cpp:307
lldb_private::Process
Definition: Process.h:340
Module.h
lldb_private::Process::GetThreadList
ThreadList & GetThreadList()
Definition: Process.h:2107
lldb_private::Process::GetTarget
Target & GetTarget()
Get the target object pointer for this module.
Definition: Process.h:1208
lldb_private::EvaluateExpressionOptions::SetTrapExceptions
void SetTrapExceptions(bool b)
Definition: Target.h:362
lldb_private::EvaluateExpressionOptions
Definition: Target.h:258
PROT_WRITE
#define PROT_WRITE
Definition: InferiorCallPOSIX.cpp:30
lldb_private::SymbolContextList
Definition: SymbolContext.h:379
lldb_private::SymbolContextList::GetContextAtIndex
bool GetContextAtIndex(size_t idx, SymbolContext &sc) const
Get accessor for a symbol context at index idx.
Definition: SymbolContext.cpp:1262
lldb_private::Target::GetScratchTypeSystemForLanguage
llvm::Expected< TypeSystem & > GetScratchTypeSystemForLanguage(lldb::LanguageType language, bool create_on_demand=true)
Definition: Target.cpp:2203
lldb::addr_t
uint64_t addr_t
Definition: lldb-types.h:83
lldb_private::ModuleFunctionSearchOptions::include_symbols
bool include_symbols
Include the symbol table.
Definition: Module.h:64
lldb_private::SymbolContext
Definition: SymbolContext.h:33
lldb_private::InferiorCallMunmap
bool InferiorCallMunmap(Process *proc, lldb::addr_t addr, lldb::addr_t length)
Definition: InferiorCallPOSIX.cpp:133
lldb_private::eMmapProtWrite
@ eMmapProtWrite
Definition: InferiorCallPOSIX.h:24
lldb_private::Target::GetImages
const ModuleList & GetImages() const
Get accessor for the images for this process.
Definition: Target.h:924
Process.h
lldb_private::ModuleFunctionSearchOptions::include_inlines
bool include_inlines
Include inlined functions.
Definition: Module.h:66
lldb_private::ThreadPlanCallFunction
Definition: ThreadPlanCallFunction.h:20
Target.h
lldb_private::Target::GetPlatform
lldb::PlatformSP GetPlatform()
Definition: Target.h:1341
Platform.h
PROT_NONE
#define PROT_NONE
Definition: InferiorCallPOSIX.cpp:28
lldb_private::Thread
Definition: Thread.h:60
lldb::eExpressionCompleted
@ eExpressionCompleted
Definition: lldb-enumerations.h:271
lldb_private::Process::GetAddressByteSize
uint32_t GetAddressByteSize() const
Definition: Process.cpp:3336
lldb_private::SymbolContextList::GetSize
uint32_t GetSize() const
Get accessor for a symbol context list size.
Definition: SymbolContext.cpp:1278
lldb_private::ConstString
Definition: ConstString.h:40
lldb_private::AddressRange
Definition: AddressRange.h:25
lldb_private::EvaluateExpressionOptions::SetTryAllThreads
void SetTryAllThreads(bool try_others=true)
Definition: Target.h:338
lldb::eBasicTypeVoid
@ eBasicTypeVoid
Definition: lldb-enumerations.h:743
lldb_private::eMmapProtExec
@ eMmapProtExec
Definition: InferiorCallPOSIX.h:22
PROT_READ
#define PROT_READ
Definition: InferiorCallPOSIX.cpp:29
lldb_private::EvaluateExpressionOptions::SetUnwindOnError
void SetUnwindOnError(bool unwind=false)
Definition: Target.h:305
PROT_EXEC
#define PROT_EXEC
Definition: InferiorCallPOSIX.cpp:31
TypeSystem.h
Address.h
ThreadPlanCallFunction.h
ValueObject.h
lldb_private::ModuleFunctionSearchOptions
Options used by Module::FindFunctions.
Definition: Module.h:62
lldb_private::EvaluateExpressionOptions::SetIgnoreBreakpoints
void SetIgnoreBreakpoints(bool ignore=false)
Definition: Target.h:309
StreamFile.h
lldb_private::Target::GetArchitecture
const ArchSpec & GetArchitecture() const
Definition: Target.h:966
lldb_private::CompilerType::GetPointerType
CompilerType GetPointerType() const
Return a new CompilerType that is a pointer to this type.
Definition: CompilerType.cpp:407
uint32_t
lldb_private::StackFrame::CalculateExecutionContext
void CalculateExecutionContext(ExecutionContext &exe_ctx) override
Reconstruct the object's execution context into sc.
Definition: StackFrame.cpp:1755
lldb_private::eMmapProtRead
@ eMmapProtRead
Definition: InferiorCallPOSIX.h:23
lldb_private::DiagnosticManager
Definition: DiagnosticManager.h:93
UINT32_MAX
#define UINT32_MAX
Definition: lldb-defines.h:31
LLDB_INVALID_ADDRESS
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:86
lldb_private::CompilerType::GetBasicTypeFromAST
CompilerType GetBasicTypeFromAST(lldb::BasicType basic_type) const
Create related types using the current type's AST.
Definition: CompilerType.cpp:474
lldb_private::CompilerType
Generic representation of a type in a programming language.
Definition: CompilerType.h:33
lldb_private::ModuleList::FindFunctions
void FindFunctions(ConstString name, lldb::FunctionNameType name_type_mask, const ModuleFunctionSearchOptions &options, SymbolContextList &sc_list) const
DiagnosticManager.h
lldb_private::InferiorCallMmap
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: InferiorCallPOSIX.cpp:37
SymbolContext.h
lldb::eLanguageTypeC
@ eLanguageTypeC
Non-standardized C, such as K&R.
Definition: lldb-enumerations.h:439
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::EvaluateExpressionOptions::SetDebug
void SetDebug(bool b)
Definition: Target.h:346
lldb_private::EvaluateExpressionOptions::SetStopOthers
void SetStopOthers(bool stop_others=true)
Definition: Target.h:342
lldb_private::Thread::GetStackFrameAtIndex
virtual lldb::StackFrameSP GetStackFrameAtIndex(uint32_t idx)
Definition: Thread.h:397
lldb_private::Process::RunThreadPlan
lldb::ExpressionResults RunThreadPlan(ExecutionContext &exe_ctx, lldb::ThreadPlanSP &thread_plan_sp, const EvaluateExpressionOptions &options, DiagnosticManager &diagnostic_manager)
Definition: Process.cpp:4646
lldb_private::eMmapProtNone
@ eMmapProtNone
Definition: InferiorCallPOSIX.h:21
lldb_private::StackFrame
Definition: StackFrame.h:40
lldb
Definition: SBAddress.h:15
lldb_private::ThreadList::GetExpressionExecutionThread
lldb::ThreadSP GetExpressionExecutionThread()
Definition: ThreadList.cpp:59
ExecutionContext.h
UINT64_MAX
#define UINT64_MAX
Definition: lldb-defines.h:35