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"
24 using namespace lldb_private;
26 bool lldb_private::InferiorCallMmap(Process *process, addr_t &allocated_addr,
27 addr_t addr, addr_t length, unsigned prot,
28 unsigned flags, addr_t fd, addr_t offset) {
29 Thread *thread = process->GetThreadList().GetSelectedThread().get();
33 const bool append = true;
34 const bool include_symbols = true;
35 const bool include_inlines = false;
36 SymbolContextList sc_list;
38 = process->GetTarget().GetImages().FindFunctions (ConstString ("mmap"),
39 eFunctionNameTypeFull,
47 if (sc_list.GetContextAtIndex(0, sc))
49 const uint32_t range_scope = eSymbolContextFunction | eSymbolContextSymbol;
50 const bool use_inline_block_range = false;
51 const bool stop_other_threads = true;
52 const bool unwind_on_error = true;
53 const bool ignore_breakpoints = true;
54 const bool try_all_threads = true;
55 const uint32_t timeout_usec = 500000;
57 addr_t prot_arg, flags_arg = 0;
58 if (prot == eMmapProtNone)
62 if (prot & eMmapProtExec)
63 prot_arg |= PROT_EXEC;
64 if (prot & eMmapProtRead)
65 prot_arg |= PROT_READ;
66 if (prot & eMmapProtWrite)
67 prot_arg |= PROT_WRITE;
70 if (flags & eMmapFlagsPrivate)
71 flags_arg |= MAP_PRIVATE;
72 if (flags & eMmapFlagsAnon)
73 flags_arg |= MAP_ANON;
75 AddressRange mmap_range;
76 if (sc.GetAddressRange(range_scope, 0, use_inline_block_range, mmap_range))
78 ClangASTContext *clang_ast_context = process->GetTarget().GetScratchClangASTContext();
79 ClangASTType clang_void_ptr_type = clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType();
80 ThreadPlanCallFunction *call_function_thread_plan
81 = new ThreadPlanCallFunction (*thread,
82 mmap_range.GetBaseAddress(),
93 lldb::ThreadPlanSP call_plan_sp (call_function_thread_plan);
96 StreamFile error_strm;
97 // This plan is a utility plan, so set it to discard itself when done.
98 call_plan_sp->SetIsMasterPlan (true);
99 call_plan_sp->SetOkayToDiscard(true);
101 StackFrame *frame = thread->GetStackFrameAtIndex (0).get();
104 ExecutionContext exe_ctx;
105 frame->CalculateExecutionContext (exe_ctx);
106 ExecutionResults result = process->RunThreadPlan (exe_ctx,
114 if (result == eExecutionCompleted)
117 allocated_addr = call_plan_sp->GetReturnValueObject()->GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
118 if (process->GetAddressByteSize() == 4)
120 if (allocated_addr == UINT32_MAX)
123 else if (process->GetAddressByteSize() == 8)
125 if (allocated_addr == UINT64_MAX)
139 bool lldb_private::InferiorCallMunmap(Process *process, addr_t addr,
141 Thread *thread = process->GetThreadList().GetSelectedThread().get();
145 const bool append = true;
146 const bool include_symbols = true;
147 const bool include_inlines = false;
148 SymbolContextList sc_list;
150 = process->GetTarget().GetImages().FindFunctions (ConstString ("munmap"),
151 eFunctionNameTypeFull,
159 if (sc_list.GetContextAtIndex(0, sc))
161 const uint32_t range_scope = eSymbolContextFunction | eSymbolContextSymbol;
162 const bool use_inline_block_range = false;
163 const bool stop_other_threads = true;
164 const bool unwind_on_error = true;
165 const bool ignore_breakpoints = true;
166 const bool try_all_threads = true;
167 const uint32_t timeout_usec = 500000;
169 AddressRange munmap_range;
170 if (sc.GetAddressRange(range_scope, 0, use_inline_block_range, munmap_range))
172 lldb::ThreadPlanSP call_plan_sp (new ThreadPlanCallFunction (*thread,
173 munmap_range.GetBaseAddress(),
182 StreamFile error_strm;
183 // This plan is a utility plan, so set it to discard itself when done.
184 call_plan_sp->SetIsMasterPlan (true);
185 call_plan_sp->SetOkayToDiscard(true);
187 StackFrame *frame = thread->GetStackFrameAtIndex (0).get();
190 ExecutionContext exe_ctx;
191 frame->CalculateExecutionContext (exe_ctx);
192 ExecutionResults result = process->RunThreadPlan (exe_ctx,
200 if (result == eExecutionCompleted)
213 bool lldb_private::InferiorCall(Process *process, const Address *address, addr_t &returned_func) {
214 Thread *thread = process->GetThreadList().GetSelectedThread().get();
215 if (thread == NULL || address == NULL)
218 const bool stop_other_threads = true;
219 const bool unwind_on_error = true;
220 const bool ignore_breakpoints = true;
221 const bool try_all_threads = true;
222 const uint32_t timeout_usec = 500000;
224 ClangASTContext *clang_ast_context = process->GetTarget().GetScratchClangASTContext();
225 ClangASTType clang_void_ptr_type = clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType();
226 ThreadPlanCallFunction *call_function_thread_plan
227 = new ThreadPlanCallFunction (*thread,
233 lldb::ThreadPlanSP call_plan_sp (call_function_thread_plan);
236 StreamFile error_strm;
237 // This plan is a utility plan, so set it to discard itself when done.
238 call_plan_sp->SetIsMasterPlan (true);
239 call_plan_sp->SetOkayToDiscard(true);
241 StackFrame *frame = thread->GetStackFrameAtIndex (0).get();
244 ExecutionContext exe_ctx;
245 frame->CalculateExecutionContext (exe_ctx);
246 ExecutionResults result = process->RunThreadPlan (exe_ctx,
254 if (result == eExecutionCompleted)
256 returned_func = call_plan_sp->GetReturnValueObject()->GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
258 if (process->GetAddressByteSize() == 4)
260 if (returned_func == UINT32_MAX)
263 else if (process->GetAddressByteSize() == 8)
265 if (returned_func == UINT64_MAX)