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/Process.h"
18 #include "lldb/Target/Target.h"
19 #include "lldb/Target/ThreadPlanCallFunction.h"
20 #include "lldb/Host/Config.h"
22 #ifndef LLDB_DISABLE_POSIX
31 #define MAP_ANON 0x1000
35 using namespace lldb_private;
38 lldb_private::InferiorCallMmap (Process *process,
39 addr_t &allocated_addr,
47 Thread *thread = process->GetThreadList().GetSelectedThread().get();
51 const bool append = true;
52 const bool include_symbols = true;
53 const bool include_inlines = false;
54 SymbolContextList sc_list;
56 = process->GetTarget().GetImages().FindFunctions (ConstString ("mmap"),
57 eFunctionNameTypeFull,
65 if (sc_list.GetContextAtIndex(0, sc))
67 const uint32_t range_scope = eSymbolContextFunction | eSymbolContextSymbol;
68 const bool use_inline_block_range = false;
69 EvaluateExpressionOptions options;
70 options.SetStopOthers(true);
71 options.SetUnwindOnError(true);
72 options.SetIgnoreBreakpoints(true);
73 options.SetTryAllThreads(true);
74 options.SetDebug (false);
75 options.SetTimeoutUsec(500000);
77 addr_t prot_arg, flags_arg = 0;
78 if (prot == eMmapProtNone)
82 if (prot & eMmapProtExec)
83 prot_arg |= PROT_EXEC;
84 if (prot & eMmapProtRead)
85 prot_arg |= PROT_READ;
86 if (prot & eMmapProtWrite)
87 prot_arg |= PROT_WRITE;
90 if (flags & eMmapFlagsPrivate)
91 flags_arg |= MAP_PRIVATE;
92 if (flags & eMmapFlagsAnon)
93 flags_arg |= MAP_ANON;
95 AddressRange mmap_range;
96 if (sc.GetAddressRange(range_scope, 0, use_inline_block_range, mmap_range))
98 ClangASTContext *clang_ast_context = process->GetTarget().GetScratchClangASTContext();
99 ClangASTType clang_void_ptr_type = clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType();
100 lldb::addr_t args[] = { addr, length, prot_arg, flags_arg, fd, offset };
101 ThreadPlanCallFunction *call_function_thread_plan
102 = new ThreadPlanCallFunction (*thread,
103 mmap_range.GetBaseAddress(),
107 lldb::ThreadPlanSP call_plan_sp (call_function_thread_plan);
110 StreamFile error_strm;
111 // This plan is a utility plan, so set it to discard itself when done.
112 call_plan_sp->SetIsMasterPlan (true);
113 call_plan_sp->SetOkayToDiscard(true);
115 StackFrame *frame = thread->GetStackFrameAtIndex (0).get();
118 ExecutionContext exe_ctx;
119 frame->CalculateExecutionContext (exe_ctx);
120 ExecutionResults result = process->RunThreadPlan (exe_ctx,
124 if (result == eExecutionCompleted)
127 allocated_addr = call_plan_sp->GetReturnValueObject()->GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
128 if (process->GetAddressByteSize() == 4)
130 if (allocated_addr == UINT32_MAX)
133 else if (process->GetAddressByteSize() == 8)
135 if (allocated_addr == UINT64_MAX)
150 lldb_private::InferiorCallMunmap (Process *process,
154 Thread *thread = process->GetThreadList().GetSelectedThread().get();
158 const bool append = true;
159 const bool include_symbols = true;
160 const bool include_inlines = false;
161 SymbolContextList sc_list;
163 = process->GetTarget().GetImages().FindFunctions (ConstString ("munmap"),
164 eFunctionNameTypeFull,
172 if (sc_list.GetContextAtIndex(0, sc))
174 const uint32_t range_scope = eSymbolContextFunction | eSymbolContextSymbol;
175 const bool use_inline_block_range = false;
176 EvaluateExpressionOptions options;
177 options.SetStopOthers(true);
178 options.SetUnwindOnError(true);
179 options.SetIgnoreBreakpoints(true);
180 options.SetTryAllThreads(true);
181 options.SetDebug (false);
182 options.SetTimeoutUsec(500000);
184 AddressRange munmap_range;
185 if (sc.GetAddressRange(range_scope, 0, use_inline_block_range, munmap_range))
187 lldb::addr_t args[] = { addr, length };
188 lldb::ThreadPlanSP call_plan_sp (new ThreadPlanCallFunction (*thread,
189 munmap_range.GetBaseAddress(),
195 StreamFile error_strm;
196 // This plan is a utility plan, so set it to discard itself when done.
197 call_plan_sp->SetIsMasterPlan (true);
198 call_plan_sp->SetOkayToDiscard(true);
200 StackFrame *frame = thread->GetStackFrameAtIndex (0).get();
203 ExecutionContext exe_ctx;
204 frame->CalculateExecutionContext (exe_ctx);
205 ExecutionResults result = process->RunThreadPlan (exe_ctx,
209 if (result == eExecutionCompleted)
222 // FIXME: This has nothing to do with Posix, it is just a convenience function that calls a
223 // function of the form "void * (*)(void)". We should find a better place to put this.
226 lldb_private::InferiorCall (Process *process,
227 const Address *address,
228 addr_t &returned_func)
230 Thread *thread = process->GetThreadList().GetSelectedThread().get();
231 if (thread == NULL || address == NULL)
234 EvaluateExpressionOptions options;
235 options.SetStopOthers(true);
236 options.SetUnwindOnError(true);
237 options.SetIgnoreBreakpoints(true);
238 options.SetTryAllThreads(true);
239 options.SetDebug (false);
240 options.SetTimeoutUsec(500000);
242 ClangASTContext *clang_ast_context = process->GetTarget().GetScratchClangASTContext();
243 ClangASTType clang_void_ptr_type = clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType();
244 ThreadPlanCallFunction *call_function_thread_plan
245 = new ThreadPlanCallFunction (*thread,
248 llvm::ArrayRef<addr_t>(),
250 lldb::ThreadPlanSP call_plan_sp (call_function_thread_plan);
253 StreamString error_strm;
254 // This plan is a utility plan, so set it to discard itself when done.
255 call_plan_sp->SetIsMasterPlan (true);
256 call_plan_sp->SetOkayToDiscard(true);
258 StackFrame *frame = thread->GetStackFrameAtIndex (0).get();
261 ExecutionContext exe_ctx;
262 frame->CalculateExecutionContext (exe_ctx);
263 ExecutionResults result = process->RunThreadPlan (exe_ctx,
267 if (result == eExecutionCompleted)
269 returned_func = call_plan_sp->GetReturnValueObject()->GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
271 if (process->GetAddressByteSize() == 4)
273 if (returned_func == UINT32_MAX)
276 else if (process->GetAddressByteSize() == 8)
278 if (returned_func == UINT64_MAX)