1 //===-- InferiorCallPOSIX.cpp -----------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
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"
22 
23 #ifndef LLDB_DISABLE_POSIX
24 #include <sys/mman.h>
25 #else
26 // define them
27 #define PROT_NONE 0
28 #define PROT_READ 1
29 #define PROT_WRITE 2
30 #define PROT_EXEC 4
31 #endif
32 
33 using namespace lldb;
34 using namespace lldb_private;
35 
36 bool
37 lldb_private::InferiorCallMmap (Process *process,
38                                 addr_t &allocated_addr,
39                                 addr_t addr,
40                                 addr_t length,
41                                 unsigned prot,
42                                 unsigned flags,
43                                 addr_t fd,
44                                 addr_t offset)
45 {
46     Thread *thread = process->GetThreadList().GetSelectedThread().get();
47     if (thread == NULL)
48         return false;
49 
50     const bool append = true;
51     const bool include_symbols = true;
52     const bool include_inlines = false;
53     SymbolContextList sc_list;
54     const uint32_t count
55       = process->GetTarget().GetImages().FindFunctions (ConstString ("mmap"),
56                                                         eFunctionNameTypeFull,
57                                                         include_symbols,
58                                                         include_inlines,
59                                                         append,
60                                                         sc_list);
61     if (count > 0)
62     {
63         SymbolContext sc;
64         if (sc_list.GetContextAtIndex(0, sc))
65         {
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 
76             addr_t prot_arg, flags_arg = 0;
77             if (prot == eMmapProtNone)
78               prot_arg = PROT_NONE;
79             else {
80               prot_arg = 0;
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;
87             }
88 
89             const ArchSpec arch =  process->GetTarget().GetArchitecture();
90             flags_arg = process->GetTarget().GetPlatform()->ConvertMmapFlagsToPlatform(arch,flags);
91 
92             AddressRange mmap_range;
93             if (sc.GetAddressRange(range_scope, 0, use_inline_block_range, mmap_range))
94             {
95                 ClangASTContext *clang_ast_context = process->GetTarget().GetScratchClangASTContext();
96                 CompilerType 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(),
100                                                                              clang_void_ptr_type,
101                                                                              args,
102                                                                              options));
103                 if (call_plan_sp)
104                 {
105                     StreamFile error_strm;
106 
107                     StackFrame *frame = thread->GetStackFrameAtIndex (0).get();
108                     if (frame)
109                     {
110                         ExecutionContext exe_ctx;
111                         frame->CalculateExecutionContext (exe_ctx);
112                         ExpressionResults result = process->RunThreadPlan (exe_ctx,
113                                                                           call_plan_sp,
114                                                                           options,
115                                                                           error_strm);
116                         if (result == eExpressionCompleted)
117                         {
118 
119                             allocated_addr = call_plan_sp->GetReturnValueObject()->GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
120                             if (process->GetAddressByteSize() == 4)
121                             {
122                                 if (allocated_addr == UINT32_MAX)
123                                     return false;
124                             }
125                             else if (process->GetAddressByteSize() == 8)
126                             {
127                                 if (allocated_addr == UINT64_MAX)
128                                     return false;
129                             }
130                             return true;
131                         }
132                     }
133                 }
134             }
135         }
136     }
137 
138     return false;
139 }
140 
141 bool
142 lldb_private::InferiorCallMunmap (Process *process,
143                                   addr_t addr,
144                                   addr_t length)
145 {
146    Thread *thread = process->GetThreadList().GetSelectedThread().get();
147    if (thread == NULL)
148        return false;
149 
150    const bool append = true;
151    const bool include_symbols = true;
152    const bool include_inlines = false;
153    SymbolContextList sc_list;
154    const uint32_t count
155      = process->GetTarget().GetImages().FindFunctions (ConstString ("munmap"),
156                                                        eFunctionNameTypeFull,
157                                                        include_symbols,
158                                                        include_inlines,
159                                                        append,
160                                                        sc_list);
161    if (count > 0)
162    {
163        SymbolContext sc;
164        if (sc_list.GetContextAtIndex(0, sc))
165        {
166             const uint32_t range_scope = eSymbolContextFunction | eSymbolContextSymbol;
167             const bool use_inline_block_range = false;
168             EvaluateExpressionOptions options;
169             options.SetStopOthers(true);
170             options.SetUnwindOnError(true);
171             options.SetIgnoreBreakpoints(true);
172             options.SetTryAllThreads(true);
173             options.SetDebug (false);
174             options.SetTimeoutUsec(500000);
175 
176             AddressRange munmap_range;
177             if (sc.GetAddressRange(range_scope, 0, use_inline_block_range, munmap_range))
178             {
179                 lldb::addr_t args[] = { addr, length };
180                 lldb::ThreadPlanSP call_plan_sp (new ThreadPlanCallFunction (*thread,
181                                                                             munmap_range.GetBaseAddress(),
182                                                                             CompilerType(),
183                                                                             args,
184                                                                             options));
185                 if (call_plan_sp)
186                 {
187                     StreamFile error_strm;
188 
189                     StackFrame *frame = thread->GetStackFrameAtIndex (0).get();
190                     if (frame)
191                     {
192                         ExecutionContext exe_ctx;
193                         frame->CalculateExecutionContext (exe_ctx);
194                         ExpressionResults result = process->RunThreadPlan (exe_ctx,
195                                                                           call_plan_sp,
196                                                                           options,
197                                                                           error_strm);
198                         if (result == eExpressionCompleted)
199                         {
200                             return true;
201                         }
202                     }
203                 }
204             }
205         }
206     }
207 
208     return false;
209 }
210 
211 // FIXME: This has nothing to do with Posix, it is just a convenience function that calls a
212 // function of the form "void * (*)(void)".  We should find a better place to put this.
213 
214 bool
215 lldb_private::InferiorCall (Process *process,
216                             const Address *address,
217                             addr_t &returned_func)
218 {
219     Thread *thread = process->GetThreadList().GetSelectedThread().get();
220     if (thread == NULL || address == NULL)
221         return false;
222 
223     EvaluateExpressionOptions options;
224     options.SetStopOthers(true);
225     options.SetUnwindOnError(true);
226     options.SetIgnoreBreakpoints(true);
227     options.SetTryAllThreads(true);
228     options.SetDebug (false);
229     options.SetTimeoutUsec(500000);
230 
231     ClangASTContext *clang_ast_context = process->GetTarget().GetScratchClangASTContext();
232     CompilerType clang_void_ptr_type = clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType();
233     lldb::ThreadPlanSP call_plan_sp (new ThreadPlanCallFunction (*thread,
234                                                                  *address,
235                                                                  clang_void_ptr_type,
236                                                                  llvm::ArrayRef<addr_t>(),
237                                                                  options));
238     if (call_plan_sp)
239     {
240         StreamString error_strm;
241 
242         StackFrame *frame = thread->GetStackFrameAtIndex (0).get();
243         if (frame)
244         {
245             ExecutionContext exe_ctx;
246             frame->CalculateExecutionContext (exe_ctx);
247             ExpressionResults result = process->RunThreadPlan (exe_ctx,
248                                                               call_plan_sp,
249                                                               options,
250                                                               error_strm);
251             if (result == eExpressionCompleted)
252             {
253                 returned_func = call_plan_sp->GetReturnValueObject()->GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
254 
255                 if (process->GetAddressByteSize() == 4)
256                 {
257                     if (returned_func == UINT32_MAX)
258                         return false;
259                 }
260                 else if (process->GetAddressByteSize() == 8)
261                 {
262                     if (returned_func == UINT64_MAX)
263                         return false;
264                 }
265                 return true;
266             }
267         }
268     }
269 
270     return false;
271 }
272