LLDB mainline
ABI.cpp
Go to the documentation of this file.
1//===-- ABI.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 "lldb/Target/ABI.h"
11#include "lldb/Core/Value.h"
15#include "lldb/Target/Target.h"
16#include "lldb/Target/Thread.h"
18#include "lldb/Utility/Log.h"
20#include "llvm/MC/TargetRegistry.h"
21#include <cctype>
22
23using namespace lldb;
24using namespace lldb_private;
25
27ABI::FindPlugin(lldb::ProcessSP process_sp, const ArchSpec &arch) {
28 for (auto create_callback : PluginManager::GetABICreateCallbacks()) {
29 if (ABISP abi_sp = create_callback(process_sp, arch))
30 return abi_sp;
31 }
32 return {};
33}
34
35ABI::~ABI() = default;
36
38 RegisterInfo &info) {
39 uint32_t count = 0;
40 const RegisterInfo *register_info_array = GetRegisterInfoArray(count);
41 if (register_info_array) {
42 uint32_t i;
43 for (i = 0; i < count; ++i) {
44 const char *reg_name = register_info_array[i].name;
45 if (reg_name == name) {
46 info = register_info_array[i];
47 return true;
48 }
49 }
50 for (i = 0; i < count; ++i) {
51 const char *reg_alt_name = register_info_array[i].alt_name;
52 if (reg_alt_name == name) {
53 info = register_info_array[i];
54 return true;
55 }
56 }
57 }
58 return false;
59}
60
62 bool persistent) const {
63 if (!ast_type.IsValid())
64 return ValueObjectSP();
65
66 ValueObjectSP return_valobj_sp;
67
68 return_valobj_sp = GetReturnValueObjectImpl(thread, ast_type);
69 if (!return_valobj_sp)
70 return return_valobj_sp;
71
72 // Now turn this into a persistent variable.
73 // FIXME: This code is duplicated from Target::EvaluateExpression, and it is
74 // used in similar form in a couple
75 // of other places. Figure out the correct Create function to do all this
76 // work.
77
78 if (persistent) {
79 Target &target = *thread.CalculateTarget();
80 PersistentExpressionState *persistent_expression_state =
82 ast_type.GetMinimumLanguage());
83
84 if (!persistent_expression_state)
85 return {};
86
87 ConstString persistent_variable_name =
88 persistent_expression_state->GetNextPersistentVariableName();
89
90 lldb::ValueObjectSP const_valobj_sp;
91
92 // Check in case our value is already a constant value
93 if (return_valobj_sp->GetIsConstant()) {
94 const_valobj_sp = return_valobj_sp;
95 const_valobj_sp->SetName(persistent_variable_name);
96 } else
97 const_valobj_sp =
98 return_valobj_sp->CreateConstantValue(persistent_variable_name);
99
100 lldb::ValueObjectSP live_valobj_sp = return_valobj_sp;
101
102 return_valobj_sp = const_valobj_sp;
103
104 ExpressionVariableSP expr_variable_sp(
105 persistent_expression_state->CreatePersistentVariable(
106 return_valobj_sp));
107
108 assert(expr_variable_sp);
109
110 // Set flags and live data as appropriate
111
112 const Value &result_value = live_valobj_sp->GetValue();
113
114 switch (result_value.GetValueType()) {
116 return {};
119 // we odon't do anything with these for now
120 break;
122 expr_variable_sp->m_flags |=
124 expr_variable_sp->m_flags |=
126 expr_variable_sp->m_flags |=
128 break;
130 expr_variable_sp->m_live_sp = live_valobj_sp;
131 expr_variable_sp->m_flags |=
133 break;
134 }
135
136 return_valobj_sp = expr_variable_sp->GetValueObject();
137 }
138 return return_valobj_sp;
139}
140
142 ProcessSP process_sp(GetProcessSP());
143
144 addr_t mask = process_sp->GetCodeAddressMask();
145 if (mask == LLDB_INVALID_ADDRESS_MASK)
146 return pc;
147
148 // Assume the high bit is used for addressing, which
149 // may not be correct on all architectures e.g. AArch64
150 // where Top Byte Ignore mode is often used to store
151 // metadata in the top byte, and b55 is the bit used for
152 // differentiating between low- and high-memory addresses.
153 // That target's ABIs need to override this method.
154 bool is_highmem = pc & (1ULL << 63);
155 return is_highmem ? pc | mask : pc & (~mask);
156}
157
159 ProcessSP process_sp(GetProcessSP());
160 addr_t mask = process_sp->GetDataAddressMask();
161 if (mask == LLDB_INVALID_ADDRESS_MASK)
162 return pc;
163
164 // Assume the high bit is used for addressing, which
165 // may not be correct on all architectures e.g. AArch64
166 // where Top Byte Ignore mode is often used to store
167 // metadata in the top byte, and b55 is the bit used for
168 // differentiating between low- and high-memory addresses.
169 // That target's ABIs need to override this method.
170 bool is_highmem = pc & (1ULL << 63);
171 return is_highmem ? pc | mask : pc & (~mask);
172}
173
174ValueObjectSP ABI::GetReturnValueObject(Thread &thread, llvm::Type &ast_type,
175 bool persistent) const {
176 ValueObjectSP return_valobj_sp;
177 return_valobj_sp = GetReturnValueObjectImpl(thread, ast_type);
178 return return_valobj_sp;
179}
180
181// specialized to work with llvm IR types
182//
183// for now we will specify a default implementation so that we don't need to
184// modify other ABIs
186 llvm::Type &ir_type) const {
187 ValueObjectSP return_valobj_sp;
188
189 /* this is a dummy and will only be called if an ABI does not override this */
190
191 return return_valobj_sp;
192}
193
195 lldb::addr_t functionAddress,
196 lldb::addr_t returnAddress, llvm::Type &returntype,
197 llvm::ArrayRef<ABI::CallArgument> args) const {
198 // dummy prepare trivial call
199 llvm_unreachable("Should never get here!");
200}
201
203 const RegisterInfo *reg_info,
205 // Did the UnwindPlan fail to give us the caller's stack pointer? The stack
206 // pointer is defined to be the same as THIS frame's CFA, so return the CFA
207 // value as the caller's stack pointer. This is true on x86-32/x86-64 at
208 // least.
210 unwind_regloc.SetIsCFAPlusOffset(0);
211 return true;
212 }
213
214 // If a volatile register is being requested, we don't want to forward the
215 // next frame's register contents up the stack -- the register is not
216 // retrievable at this frame.
217 if (RegisterIsVolatile(reg_info)) {
218 unwind_regloc.SetUndefined();
219 return true;
220 }
221
222 return false;
223}
224
225std::unique_ptr<llvm::MCRegisterInfo> ABI::MakeMCRegisterInfo(const ArchSpec &arch) {
226 const llvm::Triple &triple = arch.GetTriple();
227 std::string lookup_error;
228 const llvm::Target *target =
229 llvm::TargetRegistry::lookupTarget(triple, lookup_error);
230 if (!target) {
232 "Failed to create an llvm target for {0}: {1}", triple.str(),
233 lookup_error);
234 return nullptr;
235 }
236 std::unique_ptr<llvm::MCRegisterInfo> info_up(
237 target->createMCRegInfo(triple));
238 assert(info_up);
239 return info_up;
240}
241
243 std::vector<DynamicRegisterInfo::Register> &regs) {
244 for (DynamicRegisterInfo::Register &info : regs) {
245 if (info.regnum_ehframe != LLDB_INVALID_REGNUM &&
246 info.regnum_dwarf != LLDB_INVALID_REGNUM)
247 continue;
248
249 RegisterInfo abi_info;
250 if (!GetRegisterInfoByName(info.name.GetStringRef(), abi_info))
251 continue;
252
253 if (info.regnum_ehframe == LLDB_INVALID_REGNUM)
254 info.regnum_ehframe = abi_info.kinds[eRegisterKindEHFrame];
255 if (info.regnum_dwarf == LLDB_INVALID_REGNUM)
256 info.regnum_dwarf = abi_info.kinds[eRegisterKindDWARF];
257 if (info.regnum_generic == LLDB_INVALID_REGNUM)
258 info.regnum_generic = abi_info.kinds[eRegisterKindGeneric];
259 }
260}
261
263 std::vector<DynamicRegisterInfo::Register> &regs) {
264 for (DynamicRegisterInfo::Register &info : regs) {
265 uint32_t eh, dwarf;
266 std::tie(eh, dwarf) = GetEHAndDWARFNums(info.name.GetStringRef());
267
268 if (info.regnum_ehframe == LLDB_INVALID_REGNUM)
269 info.regnum_ehframe = eh;
270 if (info.regnum_dwarf == LLDB_INVALID_REGNUM)
271 info.regnum_dwarf = dwarf;
272 if (info.regnum_generic == LLDB_INVALID_REGNUM)
273 info.regnum_generic = GetGenericNum(info.name.GetStringRef());
274 }
275}
276
277std::pair<uint32_t, uint32_t>
278MCBasedABI::GetEHAndDWARFNums(llvm::StringRef name) {
279 std::string mc_name = GetMCName(name.str());
280 for (char &c : mc_name)
281 c = std::toupper(c);
282 int eh = -1;
283 int dwarf = -1;
284 for (unsigned reg = 0; reg < m_mc_register_info_up->getNumRegs(); ++reg) {
285 if (m_mc_register_info_up->getName(reg) == mc_name) {
286 eh = m_mc_register_info_up->getDwarfRegNum(reg, /*isEH=*/true);
287 dwarf = m_mc_register_info_up->getDwarfRegNum(reg, /*isEH=*/false);
288 break;
289 }
290 }
291 return std::pair<uint32_t, uint32_t>(eh == -1 ? LLDB_INVALID_REGNUM : eh,
293 : dwarf);
294}
295
296void MCBasedABI::MapRegisterName(std::string &name, llvm::StringRef from_prefix,
297 llvm::StringRef to_prefix) {
298 llvm::StringRef name_ref = name;
299 if (!name_ref.consume_front(from_prefix))
300 return;
301 uint64_t _;
302 if (name_ref.empty() || to_integer(name_ref, _, 10))
303 name = (to_prefix + name_ref).str();
304}
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition Log.h:369
virtual bool GetFallbackRegisterLocation(const RegisterInfo *reg_info, UnwindPlan::Row::AbstractRegisterLocation &unwind_regloc)
Definition ABI.cpp:202
virtual lldb::ValueObjectSP GetReturnValueObjectImpl(Thread &thread, CompilerType &ast_type) const =0
static lldb::ABISP FindPlugin(lldb::ProcessSP process_sp, const ArchSpec &arch)
Definition ABI.cpp:27
virtual bool PrepareTrivialCall(lldb_private::Thread &thread, lldb::addr_t sp, lldb::addr_t functionAddress, lldb::addr_t returnAddress, llvm::ArrayRef< lldb::addr_t > args) const =0
lldb::ValueObjectSP GetReturnValueObject(Thread &thread, CompilerType &type, bool persistent=true) const
Definition ABI.cpp:61
static std::unique_ptr< llvm::MCRegisterInfo > MakeMCRegisterInfo(const ArchSpec &arch)
Utility function to construct a MCRegisterInfo using the ArchSpec triple.
Definition ABI.cpp:225
virtual bool RegisterIsVolatile(const RegisterInfo *reg_info)=0
std::unique_ptr< llvm::MCRegisterInfo > m_mc_register_info_up
Definition ABI.h:168
virtual lldb::addr_t FixCodeAddress(lldb::addr_t pc)
Some targets might use bits in a code address to indicate a mode switch.
Definition ABI.cpp:141
lldb::ProcessSP GetProcessSP() const
Request to get a Process shared pointer.
Definition ABI.h:97
virtual lldb::addr_t FixDataAddress(lldb::addr_t pc)
Definition ABI.cpp:158
An architecture specification class.
Definition ArchSpec.h:32
llvm::Triple & GetTriple()
Architecture triple accessor.
Definition ArchSpec.h:457
Generic representation of a type in a programming language.
lldb::LanguageType GetMinimumLanguage()
A uniqued constant string class.
Definition ConstString.h:40
@ EVIsLLDBAllocated
This variable is resident in a location specifically allocated for it by LLDB in the target process.
@ EVNeedsAllocation
Space for this variable has yet to be allocated in the target process.
@ EVIsProgramReference
This variable is a reference to a (possibly invalid) area managed by the target program.
@ EVIsFreezeDried
This variable's authoritative version is in m_frozen_sp (for example, for statically-computed results...
virtual std::string GetMCName(std::string reg)
For the given (capitalized) lldb register name, return the name of this register in the MCRegisterInf...
Definition ABI.h:209
void AugmentRegisterInfo(std::vector< DynamicRegisterInfo::Register > &regs) override
Definition ABI.cpp:262
virtual uint32_t GetGenericNum(llvm::StringRef reg)=0
Return the generic number of the given register.
virtual std::pair< uint32_t, uint32_t > GetEHAndDWARFNums(llvm::StringRef reg)
Return eh_frame and dwarf numbers for the given register.
Definition ABI.cpp:278
static void MapRegisterName(std::string &reg, llvm::StringRef from_prefix, llvm::StringRef to_prefix)
If the register name is of the form "<from_prefix>[<number>]" then change the name to "<to_prefix>[<n...
Definition ABI.cpp:296
virtual lldb::ExpressionVariableSP CreatePersistentVariable(const lldb::ValueObjectSP &valobj_sp)=0
virtual ConstString GetNextPersistentVariableName(bool is_error=false)=0
Return a new persistent variable name with the specified prefix.
static llvm::SmallVector< ABICreateInstance > GetABICreateCallbacks()
bool GetRegisterInfoByName(llvm::StringRef name, RegisterInfo &info)
Definition ABI.cpp:37
virtual const RegisterInfo * GetRegisterInfoArray(uint32_t &count)=0
void AugmentRegisterInfo(std::vector< DynamicRegisterInfo::Register > &regs) override
Definition ABI.cpp:242
PersistentExpressionState * GetPersistentExpressionStateForLanguage(lldb::LanguageType language)
Definition Target.cpp:2685
lldb::TargetSP CalculateTarget() override
Definition Target.cpp:2589
@ HostAddress
A host address value (for memory in the process that < A is using liblldb).
Definition Value.h:52
@ FileAddress
A file address value.
Definition Value.h:47
@ LoadAddress
A load address value.
Definition Value.h:49
@ Scalar
A raw scalar value.
Definition Value.h:45
ValueType GetValueType() const
Definition Value.cpp:111
#define LLDB_INVALID_ADDRESS_MASK
Address Mask Bits not used for addressing are set to 1 in the mask; all mask bits set is an invalid v...
#define LLDB_REGNUM_GENERIC_SP
#define LLDB_INVALID_REGNUM
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:332
std::shared_ptr< lldb_private::ABI > ABISP
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP
std::shared_ptr< lldb_private::ExpressionVariable > ExpressionVariableSP
std::shared_ptr< lldb_private::Process > ProcessSP
uint64_t addr_t
Definition lldb-types.h:80
@ eRegisterKindGeneric
insn ptr reg, stack ptr reg, etc not specific to any particular target
@ eRegisterKindDWARF
the register numbers seen DWARF
@ eRegisterKindEHFrame
the register numbers seen in eh_frame
Every register is described in detail including its name, alternate name (optional),...
const char * alt_name
Alternate name of this register, can be NULL.
uint32_t kinds[lldb::kNumRegisterKinds]
Holds all of the various register numbers for all register kinds.
const char * name
Name of this register, can't be NULL.