LLDB  mainline
ABI.h
Go to the documentation of this file.
1 //===-- ABI.h ---------------------------------------------------*- C++ -*-===//
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 #ifndef LLDB_TARGET_ABI_H
10 #define LLDB_TARGET_ABI_H
11 
13 #include "lldb/Symbol/UnwindPlan.h"
15 #include "lldb/Utility/Status.h"
16 #include "lldb/lldb-private.h"
17 
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/MC/MCRegisterInfo.h"
20 
21 namespace llvm {
22 class Type;
23 }
24 
25 namespace lldb_private {
26 
27 class ABI : public PluginInterface {
28 public:
29  struct CallArgument {
30  enum eType {
31  HostPointer = 0, /* pointer to host data */
32  TargetValue, /* value is on the target or literal */
33  };
34  eType type; /* value of eType */
35  size_t size; /* size in bytes of this argument */
36 
37  lldb::addr_t value; /* literal value */
38  std::unique_ptr<uint8_t[]> data_up; /* host data pointer */
39  };
40 
41  ~ABI() override;
42 
43  virtual size_t GetRedZoneSize() const = 0;
44 
46  lldb::addr_t functionAddress,
47  lldb::addr_t returnAddress,
48  llvm::ArrayRef<lldb::addr_t> args) const = 0;
49 
50  // Prepare trivial call used from ThreadPlanFunctionCallUsingABI
51  // AD:
52  // . Because i don't want to change other ABI's this is not declared pure
53  // virtual.
54  // The dummy implementation will simply fail. Only HexagonABI will
55  // currently
56  // use this method.
57  // . Two PrepareTrivialCall's is not good design so perhaps this should be
58  // combined.
59  //
61  lldb::addr_t functionAddress,
62  lldb::addr_t returnAddress,
63  llvm::Type &prototype,
64  llvm::ArrayRef<CallArgument> args) const;
65 
66  virtual bool GetArgumentValues(Thread &thread, ValueList &values) const = 0;
67 
68  lldb::ValueObjectSP GetReturnValueObject(Thread &thread, CompilerType &type,
69  bool persistent = true) const;
70 
71  // specialized to work with llvm IR types
72  lldb::ValueObjectSP GetReturnValueObject(Thread &thread, llvm::Type &type,
73  bool persistent = true) const;
74 
75  // Set the Return value object in the current frame as though a function with
76  virtual Status SetReturnValueObject(lldb::StackFrameSP &frame_sp,
77  lldb::ValueObjectSP &new_value) = 0;
78 
79 protected:
80  // This is the method the ABI will call to actually calculate the return
81  // value. Don't put it in a persistent value object, that will be done by the
82  // ABI::GetReturnValueObject.
83  virtual lldb::ValueObjectSP
84  GetReturnValueObjectImpl(Thread &thread, CompilerType &ast_type) const = 0;
85 
86  // specialized to work with llvm IR types
87  virtual lldb::ValueObjectSP
88  GetReturnValueObjectImpl(Thread &thread, llvm::Type &ir_type) const;
89 
90  /// Request to get a Process shared pointer.
91  ///
92  /// This ABI object may not have been created with a Process object,
93  /// or the Process object may no longer be alive. Be sure to handle
94  /// the case where the shared pointer returned does not have an
95  /// object inside it.
96  lldb::ProcessSP GetProcessSP() const { return m_process_wp.lock(); }
97 
98 public:
99  virtual bool CreateFunctionEntryUnwindPlan(UnwindPlan &unwind_plan) = 0;
100 
101  virtual bool CreateDefaultUnwindPlan(UnwindPlan &unwind_plan) = 0;
102 
103  virtual bool RegisterIsVolatile(const RegisterInfo *reg_info) = 0;
104 
105  virtual bool
106  GetFallbackRegisterLocation(const RegisterInfo *reg_info,
107  UnwindPlan::Row::RegisterLocation &unwind_regloc);
108 
109  // Should take a look at a call frame address (CFA) which is just the stack
110  // pointer value upon entry to a function. ABIs usually impose alignment
111  // restrictions (4, 8 or 16 byte aligned), and zero is usually not allowed.
112  // This function should return true if "cfa" is valid call frame address for
113  // the ABI, and false otherwise. This is used by the generic stack frame
114  // unwinding code to help determine when a stack ends.
115  virtual bool CallFrameAddressIsValid(lldb::addr_t cfa) = 0;
116 
117  // Validates a possible PC value and returns true if an opcode can be at
118  // "pc".
119  virtual bool CodeAddressIsValid(lldb::addr_t pc) = 0;
120 
121  /// Some targets might use bits in a code address to indicate a mode switch.
122  /// ARM uses bit zero to signify a code address is thumb, so any ARM ABI
123  /// plug-ins would strip those bits.
124  /// @{
127  /// @}
128 
129  /// Use this method when you do not know, or do not care what kind of address
130  /// you are fixing. On platforms where there would be a difference between the
131  /// two types, it will pick the safest option.
132  ///
133  /// Its purpose is to signal that no specific choice was made and provide an
134  /// alternative to randomly picking FixCode/FixData address. Which could break
135  /// platforms where there is a difference (only Arm Thumb at this time).
137  // On Arm Thumb fixing a code address zeroes the bottom bit, so FixData is
138  // the safe choice. On any other platform (so far) code and data addresses
139  // are fixed in the same way.
140  return FixDataAddress(pc);
141  }
142 
143  llvm::MCRegisterInfo &GetMCRegisterInfo() { return *m_mc_register_info_up; }
144 
145  virtual void
146  AugmentRegisterInfo(std::vector<DynamicRegisterInfo::Register> &regs) = 0;
147 
148  virtual bool GetPointerReturnRegister(const char *&name) { return false; }
149 
150  static lldb::ABISP FindPlugin(lldb::ProcessSP process_sp, const ArchSpec &arch);
151 
152 protected:
153  ABI(lldb::ProcessSP process_sp, std::unique_ptr<llvm::MCRegisterInfo> info_up)
154  : m_process_wp(process_sp), m_mc_register_info_up(std::move(info_up)) {
155  assert(m_mc_register_info_up && "ABI must have MCRegisterInfo");
156  }
157 
158  /// Utility function to construct a MCRegisterInfo using the ArchSpec triple.
159  /// Plugins wishing to customize the construction can construct the
160  /// MCRegisterInfo themselves.
161  static std::unique_ptr<llvm::MCRegisterInfo>
162  MakeMCRegisterInfo(const ArchSpec &arch);
163 
164  lldb::ProcessWP m_process_wp;
165  std::unique_ptr<llvm::MCRegisterInfo> m_mc_register_info_up;
166 
168  return pc;
169  }
170 
171 private:
172  ABI(const ABI &) = delete;
173  const ABI &operator=(const ABI &) = delete;
174 };
175 
176 class RegInfoBasedABI : public ABI {
177 public:
178  void AugmentRegisterInfo(
179  std::vector<DynamicRegisterInfo::Register> &regs) override;
180 
181 protected:
182  using ABI::ABI;
183 
184  bool GetRegisterInfoByName(llvm::StringRef name, RegisterInfo &info);
185 
186  virtual const RegisterInfo *GetRegisterInfoArray(uint32_t &count) = 0;
187 };
188 
189 class MCBasedABI : public ABI {
190 public:
191  void AugmentRegisterInfo(
192  std::vector<DynamicRegisterInfo::Register> &regs) override;
193 
194  /// If the register name is of the form "<from_prefix>[<number>]" then change
195  /// the name to "<to_prefix>[<number>]". Otherwise, leave the name unchanged.
196  static void MapRegisterName(std::string &reg, llvm::StringRef from_prefix,
197  llvm::StringRef to_prefix);
198 
199 protected:
200  using ABI::ABI;
201 
202  /// Return eh_frame and dwarf numbers for the given register.
203  virtual std::pair<uint32_t, uint32_t> GetEHAndDWARFNums(llvm::StringRef reg);
204 
205  /// Return the generic number of the given register.
206  virtual uint32_t GetGenericNum(llvm::StringRef reg) = 0;
207 
208  /// For the given (capitalized) lldb register name, return the name of this
209  /// register in the MCRegisterInfo struct.
210  virtual std::string GetMCName(std::string reg) { return reg; }
211 };
212 
213 } // namespace lldb_private
214 
215 #endif // LLDB_TARGET_ABI_H
lldb_private::npdb::PdbSymUidKind::Type
@ Type
lldb_private::ABI::GetPointerReturnRegister
virtual bool GetPointerReturnRegister(const char *&name)
Definition: ABI.h:148
lldb_private::ABI::CallArgument::TargetValue
@ TargetValue
Definition: ABI.h:32
llvm
Definition: Debugger.h:50
lldb_private::ArchSpec
Definition: ArchSpec.h:32
lldb_private::ABI
Definition: ABI.h:27
lldb_private::RegInfoBasedABI::GetRegisterInfoByName
bool GetRegisterInfoByName(llvm::StringRef name, RegisterInfo &info)
Definition: ABI.cpp:46
lldb_private::RegInfoBasedABI::GetRegisterInfoArray
virtual const RegisterInfo * GetRegisterInfoArray(uint32_t &count)=0
lldb_private::RegInfoBasedABI::AugmentRegisterInfo
void AugmentRegisterInfo(std::vector< DynamicRegisterInfo::Register > &regs) override
Definition: ABI.cpp:218
lldb_private::PluginInterface
Definition: PluginInterface.h:16
lldb_private::ABI::CodeAddressIsValid
virtual bool CodeAddressIsValid(lldb::addr_t pc)=0
lldb_private::MCBasedABI
Definition: ABI.h:189
lldb_private::ABI::CallArgument::type
eType type
Definition: ABI.h:34
DynamicRegisterInfo.h
lldb_private::ABI::GetFallbackRegisterLocation
virtual bool GetFallbackRegisterLocation(const RegisterInfo *reg_info, UnwindPlan::Row::RegisterLocation &unwind_regloc)
Definition: ABI.cpp:178
lldb_private::ABI::CallArgument::eType
eType
Definition: ABI.h:30
lldb_private::ValueList
Definition: Value.h:157
lldb_private::MCBasedABI::GetEHAndDWARFNums
virtual std::pair< uint32_t, uint32_t > GetEHAndDWARFNums(llvm::StringRef reg)
Return eh_frame and dwarf numbers for the given register.
Definition: ABI.cpp:254
lldb::addr_t
uint64_t addr_t
Definition: lldb-types.h:83
pc
@ pc
Definition: CompactUnwindInfo.cpp:1251
lldb_private::ABI::GetRedZoneSize
virtual size_t GetRedZoneSize() const =0
lldb_private::ABI::m_process_wp
lldb::ProcessWP m_process_wp
Definition: ABI.h:164
lldb_private::ABI::RegisterIsVolatile
virtual bool RegisterIsVolatile(const RegisterInfo *reg_info)=0
lldb_private::Thread
Definition: Thread.h:61
lldb_private::ABI::AugmentRegisterInfo
virtual void AugmentRegisterInfo(std::vector< DynamicRegisterInfo::Register > &regs)=0
lldb_private::ABI::CreateFunctionEntryUnwindPlan
virtual bool CreateFunctionEntryUnwindPlan(UnwindPlan &unwind_plan)=0
lldb_private::ABI::GetReturnValueObjectImpl
virtual lldb::ValueObjectSP GetReturnValueObjectImpl(Thread &thread, CompilerType &ast_type) const =0
lldb_private::ABI::FixCodeAddress
virtual lldb::addr_t FixCodeAddress(lldb::addr_t pc, lldb::addr_t mask)
Definition: ABI.h:167
PluginInterface.h
lldb_private::ABI::GetMCRegisterInfo
llvm::MCRegisterInfo & GetMCRegisterInfo()
Definition: ABI.h:143
sp
@ sp
Definition: CompactUnwindInfo.cpp:1250
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:40
lldb_private::ABI::GetProcessSP
lldb::ProcessSP GetProcessSP() const
Request to get a Process shared pointer.
Definition: ABI.h:96
lldb_private::RegInfoBasedABI
Definition: ABI.h:176
lldb_private::ABI::SetReturnValueObject
virtual Status SetReturnValueObject(lldb::StackFrameSP &frame_sp, lldb::ValueObjectSP &new_value)=0
UnwindPlan.h
lldb_private::ABI::operator=
const ABI & operator=(const ABI &)=delete
lldb-private.h
lldb_private::UnwindPlan::Row::RegisterLocation
Definition: UnwindPlan.h:57
lldb_private::Status
Definition: Status.h:44
lldb_private::ABI::CallArgument
Definition: ABI.h:29
uint32_t
lldb_private::MCBasedABI::GetGenericNum
virtual uint32_t GetGenericNum(llvm::StringRef reg)=0
Return the generic number of the given register.
lldb_private::ABI::m_mc_register_info_up
std::unique_ptr< llvm::MCRegisterInfo > m_mc_register_info_up
Definition: ABI.h:165
lldb_private::ABI::FixAnyAddress
virtual lldb::addr_t FixAnyAddress(lldb::addr_t pc)
Use this method when you do not know, or do not care what kind of address you are fixing.
Definition: ABI.h:136
lldb_private::ABI::FixDataAddress
virtual lldb::addr_t FixDataAddress(lldb::addr_t pc)
Definition: ABI.h:126
lldb_private::ABI::FixCodeAddress
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.h:125
lldb_private::ABI::CallFrameAddressIsValid
virtual bool CallFrameAddressIsValid(lldb::addr_t cfa)=0
lldb_private::MCBasedABI::GetMCName
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:210
lldb_private::MCBasedABI::AugmentRegisterInfo
void AugmentRegisterInfo(std::vector< DynamicRegisterInfo::Register > &regs) override
Definition: ABI.cpp:238
lldb_private::ABI::CallArgument::value
lldb::addr_t value
Definition: ABI.h:37
lldb_private::ABI::FindPlugin
static lldb::ABISP FindPlugin(lldb::ProcessSP process_sp, const ArchSpec &arch)
Definition: ABI.cpp:27
lldb_private::ABI::CallArgument::HostPointer
@ HostPointer
Definition: ABI.h:31
lldb_private::CompilerType
Generic representation of a type in a programming language.
Definition: CompilerType.h:35
Status.h
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::ABI::PrepareTrivialCall
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_private::ABI::CallArgument::size
size_t size
Definition: ABI.h:35
lldb_private::ABI::CreateDefaultUnwindPlan
virtual bool CreateDefaultUnwindPlan(UnwindPlan &unwind_plan)=0
lldb_private::ABI::~ABI
~ABI() override
lldb_private::ABI::GetReturnValueObject
lldb::ValueObjectSP GetReturnValueObject(Thread &thread, CompilerType &type, bool persistent=true) const
Definition: ABI.cpp:70
lldb_private::UnwindPlan
Definition: UnwindPlan.h:53
lldb_private::ABI::GetArgumentValues
virtual bool GetArgumentValues(Thread &thread, ValueList &values) const =0
lldb_private::ABI::CallArgument::data_up
std::unique_ptr< uint8_t[]> data_up
Definition: ABI.h:38
lldb_private::ABI::ABI
ABI(lldb::ProcessSP process_sp, std::unique_ptr< llvm::MCRegisterInfo > info_up)
Definition: ABI.h:153
lldb_private::ABI::MakeMCRegisterInfo
static std::unique_ptr< llvm::MCRegisterInfo > MakeMCRegisterInfo(const ArchSpec &arch)
Utility function to construct a MCRegisterInfo using the ArchSpec triple.
Definition: ABI.cpp:201
lldb_private::MCBasedABI::MapRegisterName
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:272