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);
75 options.SetTrapExceptions(false);
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 const ArchSpec arch = process->GetTarget().GetArchitecture();
91 flags_arg = process->GetTarget().GetPlatform()->ConvertMmapFlagsToPlatform(arch,flags);
93 AddressRange mmap_range;
94 if (sc.GetAddressRange(range_scope, 0, use_inline_block_range, mmap_range))
96 ClangASTContext *clang_ast_context = process->GetTarget().GetScratchClangASTContext();
97 CompilerType clang_void_ptr_type = clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType();
98 lldb::addr_t args[] = { addr, length, prot_arg, flags_arg, fd, offset };
99 lldb::ThreadPlanSP call_plan_sp (new ThreadPlanCallFunction (*thread,
100 mmap_range.GetBaseAddress(),
106 StreamFile error_strm;
108 StackFrame *frame = thread->GetStackFrameAtIndex (0).get();
111 ExecutionContext exe_ctx;
112 frame->CalculateExecutionContext (exe_ctx);
113 ExpressionResults result = process->RunThreadPlan (exe_ctx,
117 if (result == eExpressionCompleted)
120 allocated_addr = call_plan_sp->GetReturnValueObject()->GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
121 if (process->GetAddressByteSize() == 4)
123 if (allocated_addr == UINT32_MAX)
126 else if (process->GetAddressByteSize() == 8)
128 if (allocated_addr == UINT64_MAX)
143 lldb_private::InferiorCallMunmap (Process *process,
147 Thread *thread = process->GetThreadList().GetSelectedThread().get();
151 const bool append = true;
152 const bool include_symbols = true;
153 const bool include_inlines = false;
154 SymbolContextList sc_list;
156 = process->GetTarget().GetImages().FindFunctions (ConstString ("munmap"),
157 eFunctionNameTypeFull,
165 if (sc_list.GetContextAtIndex(0, sc))
167 const uint32_t range_scope = eSymbolContextFunction | eSymbolContextSymbol;
168 const bool use_inline_block_range = false;
169 EvaluateExpressionOptions options;
170 options.SetStopOthers(true);
171 options.SetUnwindOnError(true);
172 options.SetIgnoreBreakpoints(true);
173 options.SetTryAllThreads(true);
174 options.SetDebug (false);
175 options.SetTimeoutUsec(500000);
176 options.SetTrapExceptions(false);
178 AddressRange munmap_range;
179 if (sc.GetAddressRange(range_scope, 0, use_inline_block_range, munmap_range))
181 lldb::addr_t args[] = { addr, length };
182 lldb::ThreadPlanSP call_plan_sp (new ThreadPlanCallFunction (*thread,
183 munmap_range.GetBaseAddress(),
189 StreamFile error_strm;
191 StackFrame *frame = thread->GetStackFrameAtIndex (0).get();
194 ExecutionContext exe_ctx;
195 frame->CalculateExecutionContext (exe_ctx);
196 ExpressionResults result = process->RunThreadPlan (exe_ctx,
200 if (result == eExpressionCompleted)
213 // FIXME: This has nothing to do with Posix, it is just a convenience function that calls a
214 // function of the form "void * (*)(void)". We should find a better place to put this.
217 lldb_private::InferiorCall (Process *process,
218 const Address *address,
219 addr_t &returned_func,
220 bool trap_exceptions)
222 Thread *thread = process->GetThreadList().GetSelectedThread().get();
223 if (thread == NULL || address == NULL)
226 EvaluateExpressionOptions options;
227 options.SetStopOthers(true);
228 options.SetUnwindOnError(true);
229 options.SetIgnoreBreakpoints(true);
230 options.SetTryAllThreads(true);
231 options.SetDebug (false);
232 options.SetTimeoutUsec(500000);
233 options.SetTrapExceptions(trap_exceptions);
235 ClangASTContext *clang_ast_context = process->GetTarget().GetScratchClangASTContext();
236 CompilerType clang_void_ptr_type = clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType();
237 lldb::ThreadPlanSP call_plan_sp (new ThreadPlanCallFunction (*thread,
240 llvm::ArrayRef<addr_t>(),
244 StreamString error_strm;
246 StackFrame *frame = thread->GetStackFrameAtIndex (0).get();
249 ExecutionContext exe_ctx;
250 frame->CalculateExecutionContext (exe_ctx);
251 ExpressionResults result = process->RunThreadPlan (exe_ctx,
255 if (result == eExpressionCompleted)
257 returned_func = call_plan_sp->GetReturnValueObject()->GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
259 if (process->GetAddressByteSize() == 4)
261 if (returned_func == UINT32_MAX)
264 else if (process->GetAddressByteSize() == 8)
266 if (returned_func == UINT64_MAX)