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