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/StreamFile.h"
12 #include "lldb/Core/ValueObject.h"
13 #include "lldb/Symbol/SymbolContext.h"
14 #include "lldb/Target/ExecutionContext.h"
15 #include "lldb/Target/Process.h"
16 #include "lldb/Target/Target.h"
17 #include "lldb/Target/ThreadPlanCallFunction.h"
18 
19 #include <sys/mman.h>
20 
21 using namespace lldb;
22 using namespace lldb_private;
23 
24 bool lldb_private::InferiorCallMmap(Process *process, addr_t &allocated_addr,
25                                     addr_t addr, addr_t length, unsigned prot,
26                                     unsigned flags, addr_t fd, addr_t offset) {
27     Thread *thread = process->GetThreadList().GetSelectedThread().get();
28     if (thread == NULL)
29         return false;
30 
31     const bool append = true;
32     const bool include_symbols = true;
33     const bool include_inlines = false;
34     SymbolContextList sc_list;
35     const uint32_t count
36       = process->GetTarget().GetImages().FindFunctions (ConstString ("mmap"),
37                                                         eFunctionNameTypeFull,
38                                                         include_symbols,
39                                                         include_inlines,
40                                                         append,
41                                                         sc_list);
42     if (count > 0)
43     {
44         SymbolContext sc;
45         if (sc_list.GetContextAtIndex(0, sc))
46         {
47             const uint32_t range_scope = eSymbolContextFunction | eSymbolContextSymbol;
48             const bool use_inline_block_range = false;
49             const bool stop_other_threads = true;
50             const bool discard_on_error = true;
51             const bool try_all_threads = true;
52             const uint32_t single_thread_timeout_usec = 500000;
53 
54             addr_t prot_arg, flags_arg = 0;
55             if (prot == eMmapProtNone)
56               prot_arg = PROT_NONE;
57             else {
58               prot_arg = 0;
59               if (prot & eMmapProtExec)
60                 prot_arg |= PROT_EXEC;
61               if (prot & eMmapProtRead)
62                 prot_arg |= PROT_READ;
63               if (prot & eMmapProtWrite)
64                 prot_arg |= PROT_WRITE;
65             }
66 
67             if (flags & eMmapFlagsPrivate)
68               flags_arg |= MAP_PRIVATE;
69             if (flags & eMmapFlagsAnon)
70               flags_arg |= MAP_ANON;
71 
72             AddressRange mmap_range;
73             if (sc.GetAddressRange(range_scope, 0, use_inline_block_range, mmap_range))
74             {
75                 ClangASTContext *clang_ast_context = process->GetTarget().GetScratchClangASTContext();
76                 lldb::clang_type_t clang_void_ptr_type = clang_ast_context->GetVoidPtrType(false);
77                 ThreadPlanCallFunction *call_function_thread_plan
78                   = new ThreadPlanCallFunction (*thread,
79                                                 mmap_range.GetBaseAddress(),
80                                                 ClangASTType (clang_ast_context->getASTContext(), clang_void_ptr_type),
81                                                 stop_other_threads,
82                                                 discard_on_error,
83                                                 &addr,
84                                                 &length,
85                                                 &prot_arg,
86                                                 &flags_arg,
87                                                 &fd,
88                                                 &offset);
89                 lldb::ThreadPlanSP call_plan_sp (call_function_thread_plan);
90                 if (call_plan_sp)
91                 {
92                     StreamFile error_strm;
93                     StackFrame *frame = thread->GetStackFrameAtIndex (0).get();
94                     if (frame)
95                     {
96                         ExecutionContext exe_ctx;
97                         frame->CalculateExecutionContext (exe_ctx);
98                         ExecutionResults result = process->RunThreadPlan (exe_ctx,
99                                                                           call_plan_sp,
100                                                                           stop_other_threads,
101                                                                           try_all_threads,
102                                                                           discard_on_error,
103                                                                           single_thread_timeout_usec,
104                                                                           error_strm);
105                         if (result == eExecutionCompleted)
106                         {
107 
108                             allocated_addr = call_plan_sp->GetReturnValueObject()->GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
109                             if (process->GetAddressByteSize() == 4)
110                             {
111                                 if (allocated_addr == UINT32_MAX)
112                                     return false;
113                             }
114                             return true;
115                         }
116                     }
117                 }
118             }
119         }
120     }
121 
122     return false;
123 }
124 
125 bool lldb_private::InferiorCallMunmap(Process *process, addr_t addr,
126                                       addr_t length) {
127    Thread *thread = process->GetThreadList().GetSelectedThread().get();
128    if (thread == NULL)
129        return false;
130 
131    const bool append = true;
132    const bool include_symbols = true;
133    const bool include_inlines = false;
134    SymbolContextList sc_list;
135    const uint32_t count
136      = process->GetTarget().GetImages().FindFunctions (ConstString ("munmap"),
137                                                        eFunctionNameTypeFull,
138                                                        include_symbols,
139                                                        include_inlines,
140                                                        append,
141                                                        sc_list);
142    if (count > 0)
143    {
144        SymbolContext sc;
145        if (sc_list.GetContextAtIndex(0, sc))
146        {
147            const uint32_t range_scope = eSymbolContextFunction | eSymbolContextSymbol;
148            const bool use_inline_block_range = false;
149            const bool stop_other_threads = true;
150            const bool discard_on_error = true;
151            const bool try_all_threads = true;
152            const uint32_t single_thread_timeout_usec = 500000;
153 
154            AddressRange munmap_range;
155            if (sc.GetAddressRange(range_scope, 0, use_inline_block_range, munmap_range))
156            {
157                lldb::ThreadPlanSP call_plan_sp (new ThreadPlanCallFunction (*thread,
158                                                                             munmap_range.GetBaseAddress(),
159                                                                             ClangASTType(),
160                                                                             stop_other_threads,
161                                                                             discard_on_error,
162                                                                             &addr,
163                                                                             &length));
164                if (call_plan_sp)
165                {
166                    StreamFile error_strm;
167                    StackFrame *frame = thread->GetStackFrameAtIndex (0).get();
168                    if (frame)
169                    {
170                        ExecutionContext exe_ctx;
171                        frame->CalculateExecutionContext (exe_ctx);
172                        ExecutionResults result = process->RunThreadPlan (exe_ctx,
173                                                                          call_plan_sp,
174                                                                          stop_other_threads,
175                                                                          try_all_threads,
176                                                                          discard_on_error,
177                                                                          single_thread_timeout_usec,
178                                                                          error_strm);
179                        if (result == eExecutionCompleted)
180                        {
181                            return true;
182                        }
183                    }
184                }
185            }
186        }
187    }
188 
189    return false;
190 }
191