1 //===-- InferiorCallPOSIX.cpp -----------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "InferiorCallPOSIX.h"
11 #include "lldb/Core/Address.h"
12 #include "lldb/Core/StreamFile.h"
13 #include "lldb/Core/ValueObject.h"
14 #include "lldb/Expression/DiagnosticManager.h"
15 #include "lldb/Host/Config.h"
16 #include "lldb/Symbol/ClangASTContext.h"
17 #include "lldb/Symbol/SymbolContext.h"
18 #include "lldb/Target/ExecutionContext.h"
19 #include "lldb/Target/Platform.h"
20 #include "lldb/Target/Process.h"
21 #include "lldb/Target/Target.h"
22 #include "lldb/Target/ThreadPlanCallFunction.h"
24 #ifndef LLDB_DISABLE_POSIX
35 using namespace lldb_private;
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) {
41 process->GetThreadList().GetExpressionExecutionThread().get();
45 const bool append = true;
46 const bool include_symbols = true;
47 const bool include_inlines = false;
48 SymbolContextList sc_list;
49 const uint32_t count = process->GetTarget().GetImages().FindFunctions(
50 ConstString("mmap"), eFunctionNameTypeFull, include_symbols,
51 include_inlines, append, sc_list);
54 if (sc_list.GetContextAtIndex(0, sc)) {
55 const uint32_t range_scope =
56 eSymbolContextFunction | eSymbolContextSymbol;
57 const bool use_inline_block_range = false;
58 EvaluateExpressionOptions options;
59 options.SetStopOthers(true);
60 options.SetUnwindOnError(true);
61 options.SetIgnoreBreakpoints(true);
62 options.SetTryAllThreads(true);
63 options.SetDebug(false);
64 options.SetTimeout(std::chrono::milliseconds(500));
65 options.SetTrapExceptions(false);
68 if (prot == eMmapProtNone)
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;
80 AddressRange mmap_range;
81 if (sc.GetAddressRange(range_scope, 0, use_inline_block_range,
83 ClangASTContext *clang_ast_context =
84 process->GetTarget().GetScratchClangASTContext();
85 CompilerType clang_void_ptr_type =
86 clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType();
87 const ArchSpec arch = process->GetTarget().GetArchitecture();
89 process->GetTarget().GetPlatform()->GetMmapArgumentList(
90 arch, addr, length, prot_arg, flags, fd, offset);
91 lldb::ThreadPlanSP call_plan_sp(
92 new ThreadPlanCallFunction(*thread, mmap_range.GetBaseAddress(),
93 clang_void_ptr_type, args, options));
95 DiagnosticManager diagnostics;
97 StackFrame *frame = thread->GetStackFrameAtIndex(0).get();
99 ExecutionContext exe_ctx;
100 frame->CalculateExecutionContext(exe_ctx);
101 ExpressionResults result = process->RunThreadPlan(
102 exe_ctx, call_plan_sp, options, diagnostics);
103 if (result == eExpressionCompleted) {
106 call_plan_sp->GetReturnValueObject()->GetValueAsUnsigned(
107 LLDB_INVALID_ADDRESS);
108 if (process->GetAddressByteSize() == 4) {
109 if (allocated_addr == UINT32_MAX)
111 } else if (process->GetAddressByteSize() == 8) {
112 if (allocated_addr == UINT64_MAX)
126 bool lldb_private::InferiorCallMunmap(Process *process, addr_t addr,
129 process->GetThreadList().GetExpressionExecutionThread().get();
133 const bool append = true;
134 const bool include_symbols = true;
135 const bool include_inlines = false;
136 SymbolContextList sc_list;
137 const uint32_t count = process->GetTarget().GetImages().FindFunctions(
138 ConstString("munmap"), eFunctionNameTypeFull, include_symbols,
139 include_inlines, append, sc_list);
142 if (sc_list.GetContextAtIndex(0, sc)) {
143 const uint32_t range_scope =
144 eSymbolContextFunction | eSymbolContextSymbol;
145 const bool use_inline_block_range = false;
146 EvaluateExpressionOptions options;
147 options.SetStopOthers(true);
148 options.SetUnwindOnError(true);
149 options.SetIgnoreBreakpoints(true);
150 options.SetTryAllThreads(true);
151 options.SetDebug(false);
152 options.SetTimeout(std::chrono::milliseconds(500));
153 options.SetTrapExceptions(false);
155 AddressRange munmap_range;
156 if (sc.GetAddressRange(range_scope, 0, use_inline_block_range,
158 lldb::addr_t args[] = {addr, length};
159 lldb::ThreadPlanSP call_plan_sp(
160 new ThreadPlanCallFunction(*thread, munmap_range.GetBaseAddress(),
161 CompilerType(), args, options));
163 DiagnosticManager diagnostics;
165 StackFrame *frame = thread->GetStackFrameAtIndex(0).get();
167 ExecutionContext exe_ctx;
168 frame->CalculateExecutionContext(exe_ctx);
169 ExpressionResults result = process->RunThreadPlan(
170 exe_ctx, call_plan_sp, options, diagnostics);
171 if (result == eExpressionCompleted) {
183 // FIXME: This has nothing to do with Posix, it is just a convenience function
185 // function of the form "void * (*)(void)". We should find a better place to
188 bool lldb_private::InferiorCall(Process *process, const Address *address,
189 addr_t &returned_func, bool trap_exceptions) {
191 process->GetThreadList().GetExpressionExecutionThread().get();
192 if (thread == NULL || address == NULL)
195 EvaluateExpressionOptions options;
196 options.SetStopOthers(true);
197 options.SetUnwindOnError(true);
198 options.SetIgnoreBreakpoints(true);
199 options.SetTryAllThreads(true);
200 options.SetDebug(false);
201 options.SetTimeout(std::chrono::milliseconds(500));
202 options.SetTrapExceptions(trap_exceptions);
204 ClangASTContext *clang_ast_context =
205 process->GetTarget().GetScratchClangASTContext();
206 CompilerType clang_void_ptr_type =
207 clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType();
208 lldb::ThreadPlanSP call_plan_sp(
209 new ThreadPlanCallFunction(*thread, *address, clang_void_ptr_type,
210 llvm::ArrayRef<addr_t>(), options));
212 DiagnosticManager diagnostics;
214 StackFrame *frame = thread->GetStackFrameAtIndex(0).get();
216 ExecutionContext exe_ctx;
217 frame->CalculateExecutionContext(exe_ctx);
218 ExpressionResults result =
219 process->RunThreadPlan(exe_ctx, call_plan_sp, options, diagnostics);
220 if (result == eExpressionCompleted) {
222 call_plan_sp->GetReturnValueObject()->GetValueAsUnsigned(
223 LLDB_INVALID_ADDRESS);
225 if (process->GetAddressByteSize() == 4) {
226 if (returned_func == UINT32_MAX)
228 } else if (process->GetAddressByteSize() == 8) {
229 if (returned_func == UINT64_MAX)