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/Symbol/TypeSystem.h"
16 #include "lldb/Target/Target.h"
17 #include "lldb/Target/Thread.h"
18 #include "lldb/Utility/LLDBLog.h"
19 #include "lldb/Utility/Log.h"
20 #include "llvm/MC/TargetRegistry.h"
21 #include <cctype>
22 
23 using namespace lldb;
24 using namespace lldb_private;
25 
26 ABISP
27 ABI::FindPlugin(lldb::ProcessSP process_sp, const ArchSpec &arch) {
28  ABISP abi_sp;
29  ABICreateInstance create_callback;
30 
31  for (uint32_t idx = 0;
32  (create_callback = PluginManager::GetABICreateCallbackAtIndex(idx)) !=
33  nullptr;
34  ++idx) {
35  abi_sp = create_callback(process_sp, arch);
36 
37  if (abi_sp)
38  return abi_sp;
39  }
40  abi_sp.reset();
41  return abi_sp;
42 }
43 
44 ABI::~ABI() = default;
45 
46 bool RegInfoBasedABI::GetRegisterInfoByName(llvm::StringRef name,
47  RegisterInfo &info) {
48  uint32_t count = 0;
49  const RegisterInfo *register_info_array = GetRegisterInfoArray(count);
50  if (register_info_array) {
51  uint32_t i;
52  for (i = 0; i < count; ++i) {
53  const char *reg_name = register_info_array[i].name;
54  if (reg_name == name) {
55  info = register_info_array[i];
56  return true;
57  }
58  }
59  for (i = 0; i < count; ++i) {
60  const char *reg_alt_name = register_info_array[i].alt_name;
61  if (reg_alt_name == name) {
62  info = register_info_array[i];
63  return true;
64  }
65  }
66  }
67  return false;
68 }
69 
70 ValueObjectSP ABI::GetReturnValueObject(Thread &thread, CompilerType &ast_type,
71  bool persistent) const {
72  if (!ast_type.IsValid())
73  return ValueObjectSP();
74 
75  ValueObjectSP return_valobj_sp;
76 
77  return_valobj_sp = GetReturnValueObjectImpl(thread, ast_type);
78  if (!return_valobj_sp)
79  return return_valobj_sp;
80 
81  // Now turn this into a persistent variable.
82  // FIXME: This code is duplicated from Target::EvaluateExpression, and it is
83  // used in similar form in a couple
84  // of other places. Figure out the correct Create function to do all this
85  // work.
86 
87  if (persistent) {
88  Target &target = *thread.CalculateTarget();
89  PersistentExpressionState *persistent_expression_state =
91  ast_type.GetMinimumLanguage());
92 
93  if (!persistent_expression_state)
94  return {};
95 
96  ConstString persistent_variable_name =
97  persistent_expression_state->GetNextPersistentVariableName();
98 
99  lldb::ValueObjectSP const_valobj_sp;
100 
101  // Check in case our value is already a constant value
102  if (return_valobj_sp->GetIsConstant()) {
103  const_valobj_sp = return_valobj_sp;
104  const_valobj_sp->SetName(persistent_variable_name);
105  } else
106  const_valobj_sp =
107  return_valobj_sp->CreateConstantValue(persistent_variable_name);
108 
109  lldb::ValueObjectSP live_valobj_sp = return_valobj_sp;
110 
111  return_valobj_sp = const_valobj_sp;
112 
113  ExpressionVariableSP expr_variable_sp(
114  persistent_expression_state->CreatePersistentVariable(
115  return_valobj_sp));
116 
117  assert(expr_variable_sp);
118 
119  // Set flags and live data as appropriate
120 
121  const Value &result_value = live_valobj_sp->GetValue();
122 
123  switch (result_value.GetValueType()) {
124  case Value::ValueType::Invalid:
125  return {};
126  case Value::ValueType::HostAddress:
127  case Value::ValueType::FileAddress:
128  // we odon't do anything with these for now
129  break;
130  case Value::ValueType::Scalar:
131  expr_variable_sp->m_flags |=
132  ExpressionVariable::EVIsFreezeDried;
133  expr_variable_sp->m_flags |=
134  ExpressionVariable::EVIsLLDBAllocated;
135  expr_variable_sp->m_flags |=
136  ExpressionVariable::EVNeedsAllocation;
137  break;
138  case Value::ValueType::LoadAddress:
139  expr_variable_sp->m_live_sp = live_valobj_sp;
140  expr_variable_sp->m_flags |=
141  ExpressionVariable::EVIsProgramReference;
142  break;
143  }
144 
145  return_valobj_sp = expr_variable_sp->GetValueObject();
146  }
147  return return_valobj_sp;
148 }
149 
150 ValueObjectSP ABI::GetReturnValueObject(Thread &thread, llvm::Type &ast_type,
151  bool persistent) const {
152  ValueObjectSP return_valobj_sp;
153  return_valobj_sp = GetReturnValueObjectImpl(thread, ast_type);
154  return return_valobj_sp;
155 }
156 
157 // specialized to work with llvm IR types
158 //
159 // for now we will specify a default implementation so that we don't need to
160 // modify other ABIs
161 lldb::ValueObjectSP ABI::GetReturnValueObjectImpl(Thread &thread,
162  llvm::Type &ir_type) const {
163  ValueObjectSP return_valobj_sp;
164 
165  /* this is a dummy and will only be called if an ABI does not override this */
166 
167  return return_valobj_sp;
168 }
169 
170 bool ABI::PrepareTrivialCall(Thread &thread, lldb::addr_t sp,
171  lldb::addr_t functionAddress,
172  lldb::addr_t returnAddress, llvm::Type &returntype,
173  llvm::ArrayRef<ABI::CallArgument> args) const {
174  // dummy prepare trivial call
175  llvm_unreachable("Should never get here!");
176 }
177 
178 bool ABI::GetFallbackRegisterLocation(
179  const RegisterInfo *reg_info,
180  UnwindPlan::Row::RegisterLocation &unwind_regloc) {
181  // Did the UnwindPlan fail to give us the caller's stack pointer? The stack
182  // pointer is defined to be the same as THIS frame's CFA, so return the CFA
183  // value as the caller's stack pointer. This is true on x86-32/x86-64 at
184  // least.
185  if (reg_info->kinds[eRegisterKindGeneric] == LLDB_REGNUM_GENERIC_SP) {
186  unwind_regloc.SetIsCFAPlusOffset(0);
187  return true;
188  }
189 
190  // If a volatile register is being requested, we don't want to forward the
191  // next frame's register contents up the stack -- the register is not
192  // retrievable at this frame.
193  if (RegisterIsVolatile(reg_info)) {
194  unwind_regloc.SetUndefined();
195  return true;
196  }
197 
198  return false;
199 }
200 
201 std::unique_ptr<llvm::MCRegisterInfo> ABI::MakeMCRegisterInfo(const ArchSpec &arch) {
202  std::string triple = arch.GetTriple().getTriple();
203  std::string lookup_error;
204  const llvm::Target *target =
205  llvm::TargetRegistry::lookupTarget(triple, lookup_error);
206  if (!target) {
207  LLDB_LOG(GetLog(LLDBLog::Process),
208  "Failed to create an llvm target for {0}: {1}", triple,
209  lookup_error);
210  return nullptr;
211  }
212  std::unique_ptr<llvm::MCRegisterInfo> info_up(
213  target->createMCRegInfo(triple));
214  assert(info_up);
215  return info_up;
216 }
217 
218 void RegInfoBasedABI::AugmentRegisterInfo(
219  std::vector<DynamicRegisterInfo::Register> &regs) {
220  for (DynamicRegisterInfo::Register &info : regs) {
221  if (info.regnum_ehframe != LLDB_INVALID_REGNUM &&
222  info.regnum_dwarf != LLDB_INVALID_REGNUM)
223  continue;
224 
225  RegisterInfo abi_info;
226  if (!GetRegisterInfoByName(info.name.GetStringRef(), abi_info))
227  continue;
228 
229  if (info.regnum_ehframe == LLDB_INVALID_REGNUM)
230  info.regnum_ehframe = abi_info.kinds[eRegisterKindEHFrame];
231  if (info.regnum_dwarf == LLDB_INVALID_REGNUM)
232  info.regnum_dwarf = abi_info.kinds[eRegisterKindDWARF];
233  if (info.regnum_generic == LLDB_INVALID_REGNUM)
234  info.regnum_generic = abi_info.kinds[eRegisterKindGeneric];
235  }
236 }
237 
238 void MCBasedABI::AugmentRegisterInfo(
239  std::vector<DynamicRegisterInfo::Register> &regs) {
240  for (DynamicRegisterInfo::Register &info : regs) {
241  uint32_t eh, dwarf;
242  std::tie(eh, dwarf) = GetEHAndDWARFNums(info.name.GetStringRef());
243 
244  if (info.regnum_ehframe == LLDB_INVALID_REGNUM)
245  info.regnum_ehframe = eh;
246  if (info.regnum_dwarf == LLDB_INVALID_REGNUM)
247  info.regnum_dwarf = dwarf;
248  if (info.regnum_generic == LLDB_INVALID_REGNUM)
249  info.regnum_generic = GetGenericNum(info.name.GetStringRef());
250  }
251 }
252 
253 std::pair<uint32_t, uint32_t>
254 MCBasedABI::GetEHAndDWARFNums(llvm::StringRef name) {
255  std::string mc_name = GetMCName(name.str());
256  for (char &c : mc_name)
257  c = std::toupper(c);
258  int eh = -1;
259  int dwarf = -1;
260  for (unsigned reg = 0; reg < m_mc_register_info_up->getNumRegs(); ++reg) {
261  if (m_mc_register_info_up->getName(reg) == mc_name) {
262  eh = m_mc_register_info_up->getDwarfRegNum(reg, /*isEH=*/true);
263  dwarf = m_mc_register_info_up->getDwarfRegNum(reg, /*isEH=*/false);
264  break;
265  }
266  }
267  return std::pair<uint32_t, uint32_t>(eh == -1 ? LLDB_INVALID_REGNUM : eh,
268  dwarf == -1 ? LLDB_INVALID_REGNUM
269  : dwarf);
270 }
271 
272 void MCBasedABI::MapRegisterName(std::string &name, llvm::StringRef from_prefix,
273  llvm::StringRef to_prefix) {
274  llvm::StringRef name_ref = name;
275  if (!name_ref.consume_front(from_prefix))
276  return;
277  uint64_t _;
278  if (name_ref.empty() || to_integer(name_ref, _, 10))
279  name = (to_prefix + name_ref).str();
280 }
ExpressionVariable.h
lldb_private::ArchSpec
Definition: ArchSpec.h:32
LLDB_INVALID_REGNUM
#define LLDB_INVALID_REGNUM
Definition: lldb-defines.h:79
lldb::eRegisterKindDWARF
@ eRegisterKindDWARF
the register numbers seen DWARF
Definition: lldb-enumerations.h:230
lldb_private::Value
Definition: Value.h:38
lldb::eRegisterKindGeneric
@ eRegisterKindGeneric
insn ptr reg, stack ptr reg, etc not specific to any particular target
Definition: lldb-enumerations.h:231
lldb_private::Value::GetValueType
ValueType GetValueType() const
Definition: Value.cpp:106
lldb_private::Thread::CalculateTarget
lldb::TargetSP CalculateTarget() override
Definition: Thread.cpp:1405
lldb_private::UnwindPlan::Row::RegisterLocation::SetUndefined
void SetUndefined()
Definition: UnwindPlan.h:84
lldb_private::ArchSpec::GetTriple
llvm::Triple & GetTriple()
Architecture triple accessor.
Definition: ArchSpec.h:463
lldb::addr_t
uint64_t addr_t
Definition: lldb-types.h:83
lldb_private::Target
Definition: Target.h:469
ABI.h
Target.h
lldb::eRegisterKindEHFrame
@ eRegisterKindEHFrame
the register numbers seen in eh_frame
Definition: lldb-enumerations.h:229
lldb_private::PersistentExpressionState::GetNextPersistentVariableName
virtual ConstString GetNextPersistentVariableName(bool is_error=false)=0
Return a new persistent variable name with the specified prefix.
dwarf
Definition: ABISysV_arc.cpp:61
Log.h
lldb_private::Thread
Definition: Thread.h:61
lldb_private::ConstString
Definition: ConstString.h:39
sp
@ sp
Definition: CompactUnwindInfo.cpp:1250
lldb_private::Target::GetPersistentExpressionStateForLanguage
PersistentExpressionState * GetPersistentExpressionStateForLanguage(lldb::LanguageType language)
Definition: Target.cpp:2398
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:40
Thread.h
TypeSystem.h
CompilerType.h
lldb_private::CompilerType::IsValid
bool IsValid() const
Definition: CompilerType.h:123
lldb_private::DynamicRegisterInfo::Register
Definition: DynamicRegisterInfo.h:27
lldb_private::UnwindPlan::Row::RegisterLocation
Definition: UnwindPlan.h:57
ValueObjectConstResult.h
uint32_t
LLDB_REGNUM_GENERIC_SP
#define LLDB_REGNUM_GENERIC_SP
Definition: lldb-defines.h:52
PluginManager.h
LLDB_LOG
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition: Log.h:337
lldb_private::CompilerType
Generic representation of a type in a programming language.
Definition: CompilerType.h:35
lldb_private::PersistentExpressionState
Definition: ExpressionVariable.h:203
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::CompilerType::GetMinimumLanguage
lldb::LanguageType GetMinimumLanguage()
Definition: CompilerType.cpp:338
lldb_private::UnwindPlan::Row::RegisterLocation::SetIsCFAPlusOffset
void SetIsCFAPlusOffset(int32_t offset)
Definition: UnwindPlan.h:113
lldb_private::PersistentExpressionState::CreatePersistentVariable
virtual lldb::ExpressionVariableSP CreatePersistentVariable(const lldb::ValueObjectSP &valobj_sp)=0
lldb_private::GetLog
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition: Log.h:309
lldb
Definition: SBAddress.h:15
Value.h
LLDBLog.h