LLDB mainline
InferiorCallPOSIX.cpp
Go to the documentation of this file.
1//===-- InferiorCallPOSIX.cpp ---------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "InferiorCallPOSIX.h"
10#include "lldb/Core/Address.h"
11#include "lldb/Core/Module.h"
13#include "lldb/Host/Config.h"
18#include "lldb/Target/Process.h"
19#include "lldb/Target/Target.h"
22
23#if LLDB_ENABLE_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
33using namespace lldb;
34using namespace lldb_private;
35
36bool lldb_private::InferiorCallMmap(Process *process, addr_t &allocated_addr,
37 addr_t addr, addr_t length, unsigned prot,
38 unsigned flags, addr_t fd, addr_t offset) {
39 Thread *thread =
41 if (thread == nullptr)
42 return false;
43
44 ModuleFunctionSearchOptions function_options;
45 function_options.include_symbols = true;
46 function_options.include_inlines = false;
47
48 SymbolContextList sc_list;
50 ConstString("mmap"), eFunctionNameTypeFull, function_options, sc_list);
51 const uint32_t count = sc_list.GetSize();
52 if (count > 0) {
54 if (sc_list.GetContextAtIndex(0, sc)) {
56 options.SetStopOthers(true);
57 options.SetUnwindOnError(true);
58 options.SetIgnoreBreakpoints(true);
59 options.SetTryAllThreads(true);
60 options.SetDebug(false);
61 options.SetTimeout(process->GetUtilityExpressionTimeout());
62 options.SetTrapExceptions(false);
63
64 addr_t prot_arg;
65 if (prot == eMmapProtNone)
66 prot_arg = PROT_NONE;
67 else {
68 prot_arg = 0;
69 if (prot & eMmapProtExec)
70 prot_arg |= PROT_EXEC;
71 if (prot & eMmapProtRead)
72 prot_arg |= PROT_READ;
73 if (prot & eMmapProtWrite)
74 prot_arg |= PROT_WRITE;
75 }
76
77 Address mmap_addr = sc.GetFunctionOrSymbolAddress();
78 if (mmap_addr.IsValid()) {
79 auto type_system_or_err =
82 if (!type_system_or_err) {
83 llvm::consumeError(type_system_or_err.takeError());
84 return false;
85 }
86 auto ts = *type_system_or_err;
87 if (!ts)
88 return false;
89 CompilerType void_ptr_type =
91 const ArchSpec arch = process->GetTarget().GetArchitecture();
92 MmapArgList args =
93 process->GetTarget().GetPlatform()->GetMmapArgumentList(
94 arch, addr, length, prot_arg, flags, fd, offset);
96 *thread, mmap_addr, void_ptr_type, args, options));
97 if (call_plan_sp) {
98 DiagnosticManager diagnostics;
99
100 StackFrame *frame = thread->GetStackFrameAtIndex(0).get();
101 if (frame) {
102 ExecutionContext exe_ctx;
103 frame->CalculateExecutionContext(exe_ctx);
104 ExpressionResults result = process->RunThreadPlan(
105 exe_ctx, call_plan_sp, options, diagnostics);
106 if (result == eExpressionCompleted) {
107
108 allocated_addr =
109 call_plan_sp->GetReturnValueObject()->GetValueAsUnsigned(
111 if (process->GetAddressByteSize() == 4) {
112 if (allocated_addr == UINT32_MAX)
113 return false;
114 } else if (process->GetAddressByteSize() == 8) {
115 if (allocated_addr == UINT64_MAX)
116 return false;
117 }
118 return true;
119 }
120 }
121 }
122 }
123 }
124 }
125
126 return false;
127}
128
130 addr_t length) {
131 Thread *thread =
133 if (thread == nullptr)
134 return false;
135
136 ModuleFunctionSearchOptions function_options;
137 function_options.include_symbols = true;
138 function_options.include_inlines = false;
139
140 SymbolContextList sc_list;
141 process->GetTarget().GetImages().FindFunctions(
142 ConstString("munmap"), eFunctionNameTypeFull, function_options, sc_list);
143 const uint32_t count = sc_list.GetSize();
144 if (count > 0) {
145 SymbolContext sc;
146 if (sc_list.GetContextAtIndex(0, sc)) {
148 options.SetStopOthers(true);
149 options.SetUnwindOnError(true);
150 options.SetIgnoreBreakpoints(true);
151 options.SetTryAllThreads(true);
152 options.SetDebug(false);
153 options.SetTimeout(process->GetUtilityExpressionTimeout());
154 options.SetTrapExceptions(false);
155
156 Address munmap_addr = sc.GetFunctionOrSymbolAddress();
157 if (munmap_addr.IsValid()) {
158 lldb::addr_t args[] = {addr, length};
160 *thread, munmap_addr, CompilerType(), args, options));
161 if (call_plan_sp) {
162 DiagnosticManager diagnostics;
163
164 StackFrame *frame = thread->GetStackFrameAtIndex(0).get();
165 if (frame) {
166 ExecutionContext exe_ctx;
167 frame->CalculateExecutionContext(exe_ctx);
168 ExpressionResults result = process->RunThreadPlan(
169 exe_ctx, call_plan_sp, options, diagnostics);
170 if (result == eExpressionCompleted) {
171 return true;
172 }
173 }
174 }
175 }
176 }
177 }
178
179 return false;
180}
#define PROT_READ
#define PROT_WRITE
#define PROT_NONE
#define PROT_EXEC
A section + offset based address class.
Definition Address.h:62
bool IsValid() const
Check if the object state is valid.
Definition Address.h:355
An architecture specification class.
Definition ArchSpec.h:31
Generic representation of a type in a programming language.
CompilerType GetBasicTypeFromAST(lldb::BasicType basic_type) const
Create related types using the current type's AST.
CompilerType GetPointerType() const
Return a new CompilerType that is a pointer to this type.
A uniqued constant string class.
Definition ConstString.h:40
void SetUnwindOnError(bool unwind=false)
Definition Target.h:371
void SetTryAllThreads(bool try_others=true)
Definition Target.h:404
void SetTimeout(const Timeout< std::micro > &timeout)
Definition Target.h:392
void SetStopOthers(bool stop_others=true)
Definition Target.h:408
void SetIgnoreBreakpoints(bool ignore=false)
Definition Target.h:375
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
void FindFunctions(ConstString name, lldb::FunctionNameType name_type_mask, const ModuleFunctionSearchOptions &options, SymbolContextList &sc_list) const
std::chrono::seconds GetUtilityExpressionTimeout() const
Definition Process.cpp:324
A plug-in interface definition class for debugging a process.
Definition Process.h:357
ThreadList & GetThreadList()
Definition Process.h:2253
lldb::ExpressionResults RunThreadPlan(ExecutionContext &exe_ctx, lldb::ThreadPlanSP &thread_plan_sp, const EvaluateExpressionOptions &options, DiagnosticManager &diagnostic_manager)
Definition Process.cpp:4970
uint32_t GetAddressByteSize() const
Definition Process.cpp:3620
Target & GetTarget()
Get the target object pointer for this module.
Definition Process.h:1270
This base class provides an interface to stack frames.
Definition StackFrame.h:44
void CalculateExecutionContext(ExecutionContext &exe_ctx) override
Reconstruct the object's execution context into sc.
Defines a list of symbol context objects.
bool GetContextAtIndex(size_t idx, SymbolContext &sc) const
Get accessor for a symbol context at index idx.
uint32_t GetSize() const
Get accessor for a symbol context list size.
Defines a symbol context baton that can be handed other debug core functions.
Address GetFunctionOrSymbolAddress() const
Get the address of the function or symbol represented by this symbol context.
llvm::Expected< lldb::TypeSystemSP > GetScratchTypeSystemForLanguage(lldb::LanguageType language, bool create_on_demand=true)
Definition Target.cpp:2609
lldb::PlatformSP GetPlatform()
Definition Target.h:1521
const ModuleList & GetImages() const
Get accessor for the images for this process.
Definition Target.h:1025
const ArchSpec & GetArchitecture() const
Definition Target.h:1067
lldb::ThreadSP GetExpressionExecutionThread()
#define UINT64_MAX
#define LLDB_INVALID_ADDRESS
#define UINT32_MAX
A class that represents a running process on the host machine.
bool InferiorCallMunmap(Process *proc, lldb::addr_t addr, lldb::addr_t length)
llvm::SmallVector< lldb::addr_t, 6 > MmapArgList
Definition Platform.h:64
bool InferiorCallMmap(Process *proc, lldb::addr_t &allocated_addr, lldb::addr_t addr, lldb::addr_t length, unsigned prot, unsigned flags, lldb::addr_t fd, lldb::addr_t offset)
std::shared_ptr< lldb_private::ThreadPlan > ThreadPlanSP
@ eLanguageTypeC
Non-standardized C, such as K&R.
ExpressionResults
The results of expression evaluation.
@ eExpressionCompleted
uint64_t addr_t
Definition lldb-types.h:80
Options used by Module::FindFunctions.
Definition Module.h:66
bool include_inlines
Include inlined functions.
Definition Module.h:70
bool include_symbols
Include the symbol table.
Definition Module.h:68