InferiorCallPOSIX.cpp revision 322326
1254721Semaste//===-- InferiorCallPOSIX.cpp -----------------------------------*- C++ -*-===//
2254721Semaste//
3254721Semaste//                     The LLVM Compiler Infrastructure
4254721Semaste//
5254721Semaste// This file is distributed under the University of Illinois Open Source
6254721Semaste// License. See LICENSE.TXT for details.
7254721Semaste//
8254721Semaste//===----------------------------------------------------------------------===//
9254721Semaste
10254721Semaste#include "InferiorCallPOSIX.h"
11254721Semaste#include "lldb/Core/Address.h"
12254721Semaste#include "lldb/Core/StreamFile.h"
13254721Semaste#include "lldb/Core/ValueObject.h"
14309124Sdim#include "lldb/Expression/DiagnosticManager.h"
15309124Sdim#include "lldb/Host/Config.h"
16254721Semaste#include "lldb/Symbol/ClangASTContext.h"
17254721Semaste#include "lldb/Symbol/SymbolContext.h"
18254721Semaste#include "lldb/Target/ExecutionContext.h"
19288943Sdim#include "lldb/Target/Platform.h"
20254721Semaste#include "lldb/Target/Process.h"
21254721Semaste#include "lldb/Target/Target.h"
22254721Semaste#include "lldb/Target/ThreadPlanCallFunction.h"
23254721Semaste
24258054Semaste#ifndef LLDB_DISABLE_POSIX
25254721Semaste#include <sys/mman.h>
26258054Semaste#else
27258054Semaste// define them
28258054Semaste#define PROT_NONE 0
29258054Semaste#define PROT_READ 1
30258054Semaste#define PROT_WRITE 2
31258054Semaste#define PROT_EXEC 4
32258054Semaste#endif
33254721Semaste
34254721Semasteusing namespace lldb;
35254721Semasteusing namespace lldb_private;
36254721Semaste
37314564Sdimbool lldb_private::InferiorCallMmap(Process *process, addr_t &allocated_addr,
38314564Sdim                                    addr_t addr, addr_t length, unsigned prot,
39314564Sdim                                    unsigned flags, addr_t fd, addr_t offset) {
40314564Sdim  Thread *thread =
41314564Sdim      process->GetThreadList().GetExpressionExecutionThread().get();
42314564Sdim  if (thread == NULL)
43314564Sdim    return false;
44254721Semaste
45314564Sdim  const bool append = true;
46314564Sdim  const bool include_symbols = true;
47314564Sdim  const bool include_inlines = false;
48314564Sdim  SymbolContextList sc_list;
49314564Sdim  const uint32_t count = process->GetTarget().GetImages().FindFunctions(
50314564Sdim      ConstString("mmap"), eFunctionNameTypeFull, include_symbols,
51314564Sdim      include_inlines, append, sc_list);
52314564Sdim  if (count > 0) {
53314564Sdim    SymbolContext sc;
54314564Sdim    if (sc_list.GetContextAtIndex(0, sc)) {
55314564Sdim      const uint32_t range_scope =
56314564Sdim          eSymbolContextFunction | eSymbolContextSymbol;
57314564Sdim      const bool use_inline_block_range = false;
58314564Sdim      EvaluateExpressionOptions options;
59314564Sdim      options.SetStopOthers(true);
60314564Sdim      options.SetUnwindOnError(true);
61314564Sdim      options.SetIgnoreBreakpoints(true);
62314564Sdim      options.SetTryAllThreads(true);
63314564Sdim      options.SetDebug(false);
64314564Sdim      options.SetTimeout(std::chrono::milliseconds(500));
65314564Sdim      options.SetTrapExceptions(false);
66254721Semaste
67322326Semaste      addr_t prot_arg;
68314564Sdim      if (prot == eMmapProtNone)
69314564Sdim        prot_arg = PROT_NONE;
70314564Sdim      else {
71314564Sdim        prot_arg = 0;
72314564Sdim        if (prot & eMmapProtExec)
73314564Sdim          prot_arg |= PROT_EXEC;
74314564Sdim        if (prot & eMmapProtRead)
75314564Sdim          prot_arg |= PROT_READ;
76314564Sdim        if (prot & eMmapProtWrite)
77314564Sdim          prot_arg |= PROT_WRITE;
78314564Sdim      }
79254721Semaste
80314564Sdim      AddressRange mmap_range;
81314564Sdim      if (sc.GetAddressRange(range_scope, 0, use_inline_block_range,
82314564Sdim                             mmap_range)) {
83314564Sdim        ClangASTContext *clang_ast_context =
84314564Sdim            process->GetTarget().GetScratchClangASTContext();
85314564Sdim        CompilerType clang_void_ptr_type =
86314564Sdim            clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType();
87322326Semaste        const ArchSpec arch = process->GetTarget().GetArchitecture();
88322326Semaste        MmapArgList args =
89322326Semaste            process->GetTarget().GetPlatform()->GetMmapArgumentList(
90322326Semaste                arch, addr, length, prot_arg, flags, fd, offset);
91314564Sdim        lldb::ThreadPlanSP call_plan_sp(
92314564Sdim            new ThreadPlanCallFunction(*thread, mmap_range.GetBaseAddress(),
93314564Sdim                                       clang_void_ptr_type, args, options));
94314564Sdim        if (call_plan_sp) {
95314564Sdim          DiagnosticManager diagnostics;
96309124Sdim
97314564Sdim          StackFrame *frame = thread->GetStackFrameAtIndex(0).get();
98314564Sdim          if (frame) {
99314564Sdim            ExecutionContext exe_ctx;
100314564Sdim            frame->CalculateExecutionContext(exe_ctx);
101314564Sdim            ExpressionResults result = process->RunThreadPlan(
102314564Sdim                exe_ctx, call_plan_sp, options, diagnostics);
103314564Sdim            if (result == eExpressionCompleted) {
104309124Sdim
105314564Sdim              allocated_addr =
106314564Sdim                  call_plan_sp->GetReturnValueObject()->GetValueAsUnsigned(
107314564Sdim                      LLDB_INVALID_ADDRESS);
108314564Sdim              if (process->GetAddressByteSize() == 4) {
109314564Sdim                if (allocated_addr == UINT32_MAX)
110314564Sdim                  return false;
111314564Sdim              } else if (process->GetAddressByteSize() == 8) {
112314564Sdim                if (allocated_addr == UINT64_MAX)
113314564Sdim                  return false;
114314564Sdim              }
115314564Sdim              return true;
116254721Semaste            }
117314564Sdim          }
118254721Semaste        }
119314564Sdim      }
120254721Semaste    }
121314564Sdim  }
122254721Semaste
123314564Sdim  return false;
124254721Semaste}
125254721Semaste
126314564Sdimbool lldb_private::InferiorCallMunmap(Process *process, addr_t addr,
127314564Sdim                                      addr_t length) {
128314564Sdim  Thread *thread =
129314564Sdim      process->GetThreadList().GetExpressionExecutionThread().get();
130314564Sdim  if (thread == NULL)
131314564Sdim    return false;
132309124Sdim
133314564Sdim  const bool append = true;
134314564Sdim  const bool include_symbols = true;
135314564Sdim  const bool include_inlines = false;
136314564Sdim  SymbolContextList sc_list;
137314564Sdim  const uint32_t count = process->GetTarget().GetImages().FindFunctions(
138314564Sdim      ConstString("munmap"), eFunctionNameTypeFull, include_symbols,
139314564Sdim      include_inlines, append, sc_list);
140314564Sdim  if (count > 0) {
141314564Sdim    SymbolContext sc;
142314564Sdim    if (sc_list.GetContextAtIndex(0, sc)) {
143314564Sdim      const uint32_t range_scope =
144314564Sdim          eSymbolContextFunction | eSymbolContextSymbol;
145314564Sdim      const bool use_inline_block_range = false;
146314564Sdim      EvaluateExpressionOptions options;
147314564Sdim      options.SetStopOthers(true);
148314564Sdim      options.SetUnwindOnError(true);
149314564Sdim      options.SetIgnoreBreakpoints(true);
150314564Sdim      options.SetTryAllThreads(true);
151314564Sdim      options.SetDebug(false);
152314564Sdim      options.SetTimeout(std::chrono::milliseconds(500));
153314564Sdim      options.SetTrapExceptions(false);
154314564Sdim
155314564Sdim      AddressRange munmap_range;
156314564Sdim      if (sc.GetAddressRange(range_scope, 0, use_inline_block_range,
157314564Sdim                             munmap_range)) {
158314564Sdim        lldb::addr_t args[] = {addr, length};
159314564Sdim        lldb::ThreadPlanSP call_plan_sp(
160314564Sdim            new ThreadPlanCallFunction(*thread, munmap_range.GetBaseAddress(),
161314564Sdim                                       CompilerType(), args, options));
162314564Sdim        if (call_plan_sp) {
163314564Sdim          DiagnosticManager diagnostics;
164314564Sdim
165314564Sdim          StackFrame *frame = thread->GetStackFrameAtIndex(0).get();
166314564Sdim          if (frame) {
167314564Sdim            ExecutionContext exe_ctx;
168314564Sdim            frame->CalculateExecutionContext(exe_ctx);
169314564Sdim            ExpressionResults result = process->RunThreadPlan(
170314564Sdim                exe_ctx, call_plan_sp, options, diagnostics);
171314564Sdim            if (result == eExpressionCompleted) {
172314564Sdim              return true;
173258884Semaste            }
174314564Sdim          }
175258884Semaste        }
176314564Sdim      }
177258884Semaste    }
178314564Sdim  }
179254721Semaste
180314564Sdim  return false;
181254721Semaste}
182254721Semaste
183314564Sdim// FIXME: This has nothing to do with Posix, it is just a convenience function
184314564Sdim// that calls a
185314564Sdim// function of the form "void * (*)(void)".  We should find a better place to
186314564Sdim// put this.
187262528Semaste
188314564Sdimbool lldb_private::InferiorCall(Process *process, const Address *address,
189314564Sdim                                addr_t &returned_func, bool trap_exceptions) {
190314564Sdim  Thread *thread =
191314564Sdim      process->GetThreadList().GetExpressionExecutionThread().get();
192314564Sdim  if (thread == NULL || address == NULL)
193314564Sdim    return false;
194254721Semaste
195314564Sdim  EvaluateExpressionOptions options;
196314564Sdim  options.SetStopOthers(true);
197314564Sdim  options.SetUnwindOnError(true);
198314564Sdim  options.SetIgnoreBreakpoints(true);
199314564Sdim  options.SetTryAllThreads(true);
200314564Sdim  options.SetDebug(false);
201314564Sdim  options.SetTimeout(std::chrono::milliseconds(500));
202314564Sdim  options.SetTrapExceptions(trap_exceptions);
203254721Semaste
204314564Sdim  ClangASTContext *clang_ast_context =
205314564Sdim      process->GetTarget().GetScratchClangASTContext();
206314564Sdim  CompilerType clang_void_ptr_type =
207314564Sdim      clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType();
208314564Sdim  lldb::ThreadPlanSP call_plan_sp(
209314564Sdim      new ThreadPlanCallFunction(*thread, *address, clang_void_ptr_type,
210314564Sdim                                 llvm::ArrayRef<addr_t>(), options));
211314564Sdim  if (call_plan_sp) {
212314564Sdim    DiagnosticManager diagnostics;
213254721Semaste
214314564Sdim    StackFrame *frame = thread->GetStackFrameAtIndex(0).get();
215314564Sdim    if (frame) {
216314564Sdim      ExecutionContext exe_ctx;
217314564Sdim      frame->CalculateExecutionContext(exe_ctx);
218314564Sdim      ExpressionResults result =
219314564Sdim          process->RunThreadPlan(exe_ctx, call_plan_sp, options, diagnostics);
220314564Sdim      if (result == eExpressionCompleted) {
221314564Sdim        returned_func =
222314564Sdim            call_plan_sp->GetReturnValueObject()->GetValueAsUnsigned(
223314564Sdim                LLDB_INVALID_ADDRESS);
224254721Semaste
225314564Sdim        if (process->GetAddressByteSize() == 4) {
226314564Sdim          if (returned_func == UINT32_MAX)
227314564Sdim            return false;
228314564Sdim        } else if (process->GetAddressByteSize() == 8) {
229314564Sdim          if (returned_func == UINT64_MAX)
230314564Sdim            return false;
231254721Semaste        }
232314564Sdim        return true;
233314564Sdim      }
234254721Semaste    }
235314564Sdim  }
236254721Semaste
237314564Sdim  return false;
238254721Semaste}
239