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/Symbol/ClangASTContext.h"
15 #include "lldb/Symbol/SymbolContext.h"
16 #include "lldb/Target/ExecutionContext.h"
17 #include "lldb/Target/Platform.h"
18 #include "lldb/Target/Process.h"
19 #include "lldb/Target/Target.h"
20 #include "lldb/Target/ThreadPlanCallFunction.h"
21 #include "lldb/Host/Config.h"
23 #ifndef LLDB_DISABLE_POSIX
34 using namespace lldb_private;
37 lldb_private::InferiorCallMmap (Process *process,
38 addr_t &allocated_addr,
46 Thread *thread = process->GetThreadList().GetSelectedThread().get();
50 const bool append = true;
51 const bool include_symbols = true;
52 const bool include_inlines = false;
53 SymbolContextList sc_list;
55 = process->GetTarget().GetImages().FindFunctions (ConstString ("mmap"),
56 eFunctionNameTypeFull,
64 if (sc_list.GetContextAtIndex(0, sc))
66 const uint32_t range_scope = eSymbolContextFunction | eSymbolContextSymbol;
67 const bool use_inline_block_range = false;
68 EvaluateExpressionOptions options;
69 options.SetStopOthers(true);
70 options.SetUnwindOnError(true);
71 options.SetIgnoreBreakpoints(true);
72 options.SetTryAllThreads(true);
73 options.SetDebug (false);
74 options.SetTimeoutUsec(500000);
76 addr_t prot_arg, flags_arg = 0;
77 if (prot == eMmapProtNone)
81 if (prot & eMmapProtExec)
82 prot_arg |= PROT_EXEC;
83 if (prot & eMmapProtRead)
84 prot_arg |= PROT_READ;
85 if (prot & eMmapProtWrite)
86 prot_arg |= PROT_WRITE;
89 const ArchSpec arch = process->GetTarget().GetArchitecture();
90 flags_arg = process->GetTarget().GetPlatform()->ConvertMmapFlagsToPlatform(arch,flags);
92 AddressRange mmap_range;
93 if (sc.GetAddressRange(range_scope, 0, use_inline_block_range, mmap_range))
95 ClangASTContext *clang_ast_context = process->GetTarget().GetScratchClangASTContext();
96 ClangASTType clang_void_ptr_type = clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType();
97 lldb::addr_t args[] = { addr, length, prot_arg, flags_arg, fd, offset };
98 lldb::ThreadPlanSP call_plan_sp (new ThreadPlanCallFunction (*thread,
99 mmap_range.GetBaseAddress(),
105 StreamFile error_strm;
106 // This plan is a utility plan, so set it to discard itself when done.
107 call_plan_sp->SetIsMasterPlan (true);
108 call_plan_sp->SetOkayToDiscard(true);
110 StackFrame *frame = thread->GetStackFrameAtIndex (0).get();
113 ExecutionContext exe_ctx;
114 frame->CalculateExecutionContext (exe_ctx);
115 ExpressionResults result = process->RunThreadPlan (exe_ctx,
119 if (result == eExpressionCompleted)
122 allocated_addr = call_plan_sp->GetReturnValueObject()->GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
123 if (process->GetAddressByteSize() == 4)
125 if (allocated_addr == UINT32_MAX)
128 else if (process->GetAddressByteSize() == 8)
130 if (allocated_addr == UINT64_MAX)
145 lldb_private::InferiorCallMunmap (Process *process,
149 Thread *thread = process->GetThreadList().GetSelectedThread().get();
153 const bool append = true;
154 const bool include_symbols = true;
155 const bool include_inlines = false;
156 SymbolContextList sc_list;
158 = process->GetTarget().GetImages().FindFunctions (ConstString ("munmap"),
159 eFunctionNameTypeFull,
167 if (sc_list.GetContextAtIndex(0, sc))
169 const uint32_t range_scope = eSymbolContextFunction | eSymbolContextSymbol;
170 const bool use_inline_block_range = false;
171 EvaluateExpressionOptions options;
172 options.SetStopOthers(true);
173 options.SetUnwindOnError(true);
174 options.SetIgnoreBreakpoints(true);
175 options.SetTryAllThreads(true);
176 options.SetDebug (false);
177 options.SetTimeoutUsec(500000);
179 AddressRange munmap_range;
180 if (sc.GetAddressRange(range_scope, 0, use_inline_block_range, munmap_range))
182 lldb::addr_t args[] = { addr, length };
183 lldb::ThreadPlanSP call_plan_sp (new ThreadPlanCallFunction (*thread,
184 munmap_range.GetBaseAddress(),
190 StreamFile error_strm;
191 // This plan is a utility plan, so set it to discard itself when done.
192 call_plan_sp->SetIsMasterPlan (true);
193 call_plan_sp->SetOkayToDiscard(true);
195 StackFrame *frame = thread->GetStackFrameAtIndex (0).get();
198 ExecutionContext exe_ctx;
199 frame->CalculateExecutionContext (exe_ctx);
200 ExpressionResults result = process->RunThreadPlan (exe_ctx,
204 if (result == eExpressionCompleted)
217 // FIXME: This has nothing to do with Posix, it is just a convenience function that calls a
218 // function of the form "void * (*)(void)". We should find a better place to put this.
221 lldb_private::InferiorCall (Process *process,
222 const Address *address,
223 addr_t &returned_func)
225 Thread *thread = process->GetThreadList().GetSelectedThread().get();
226 if (thread == NULL || address == NULL)
229 EvaluateExpressionOptions options;
230 options.SetStopOthers(true);
231 options.SetUnwindOnError(true);
232 options.SetIgnoreBreakpoints(true);
233 options.SetTryAllThreads(true);
234 options.SetDebug (false);
235 options.SetTimeoutUsec(500000);
237 ClangASTContext *clang_ast_context = process->GetTarget().GetScratchClangASTContext();
238 ClangASTType clang_void_ptr_type = clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType();
239 lldb::ThreadPlanSP call_plan_sp (new ThreadPlanCallFunction (*thread,
242 llvm::ArrayRef<addr_t>(),
246 StreamString error_strm;
247 // This plan is a utility plan, so set it to discard itself when done.
248 call_plan_sp->SetIsMasterPlan (true);
249 call_plan_sp->SetOkayToDiscard(true);
251 StackFrame *frame = thread->GetStackFrameAtIndex (0).get();
254 ExecutionContext exe_ctx;
255 frame->CalculateExecutionContext (exe_ctx);
256 ExpressionResults result = process->RunThreadPlan (exe_ctx,
260 if (result == eExpressionCompleted)
262 returned_func = call_plan_sp->GetReturnValueObject()->GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
264 if (process->GetAddressByteSize() == 4)
266 if (returned_func == UINT32_MAX)
269 else if (process->GetAddressByteSize() == 8)
271 if (returned_func == UINT64_MAX)