LLDB  mainline
DWARFCallFrameInfo.h
Go to the documentation of this file.
1 //===-- DWARFCallFrameInfo.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_SYMBOL_DWARFCALLFRAMEINFO_H
10 #define LLDB_SYMBOL_DWARFCALLFRAMEINFO_H
11 
12 #include <map>
13 #include <mutex>
14 
15 #include "lldb/Core/AddressRange.h"
16 #include "lldb/Core/dwarf.h"
17 #include "lldb/Symbol/ObjectFile.h"
18 #include "lldb/Symbol/UnwindPlan.h"
19 #include "lldb/Utility/Flags.h"
20 #include "lldb/Utility/RangeMap.h"
21 #include "lldb/Utility/VMRange.h"
22 #include "lldb/lldb-private.h"
23 
24 namespace lldb_private {
25 
26 // DWARFCallFrameInfo is a class which can read eh_frame and DWARF Call Frame
27 // Information FDEs. It stores little information internally. Only two APIs
28 // are exported - one to find the high/low pc values of a function given a text
29 // address via the information in the eh_frame / debug_frame, and one to
30 // generate an UnwindPlan based on the FDE in the eh_frame / debug_frame
31 // section.
32 
34 public:
35  enum Type { EH, DWARF };
36 
37  DWARFCallFrameInfo(ObjectFile &objfile, lldb::SectionSP &section, Type type);
38 
39  ~DWARFCallFrameInfo() = default;
40 
41  // Locate an AddressRange that includes the provided Address in this object's
42  // eh_frame/debug_info Returns true if a range is found to cover that
43  // address.
44  bool GetAddressRange(Address addr, AddressRange &range);
45 
46  /// Return an UnwindPlan based on the call frame information encoded in the
47  /// FDE of this DWARFCallFrameInfo section. The returned plan will be valid
48  /// (at least) for the given address.
49  bool GetUnwindPlan(const Address &addr, UnwindPlan &unwind_plan);
50 
51  /// Return an UnwindPlan based on the call frame information encoded in the
52  /// FDE of this DWARFCallFrameInfo section. The returned plan will be valid
53  /// (at least) for some address in the given range.
54  bool GetUnwindPlan(const AddressRange &range, UnwindPlan &unwind_plan);
55 
57 
58  // Build a vector of file address and size for all functions in this Module
59  // based on the eh_frame FDE entries.
60  //
61  // The eh_frame information can be a useful source of file address and size
62  // of the functions in a Module. Often a binary's non-exported symbols are
63  // stripped before shipping so lldb won't know the start addr / size of many
64  // functions in the Module. But the eh_frame can help to give the addresses
65  // of these stripped symbols, at least.
66  //
67  // \param[out] function_info
68  // A vector provided by the caller is filled out. May be empty if no
69  // FDEs/no eh_frame
70  // is present in this Module.
71 
72  void
74 
75  void ForEachFDEEntries(
76  const std::function<bool(lldb::addr_t, uint32_t, dw_offset_t)> &callback);
77 
78 private:
79  enum { CFI_AUG_MAX_SIZE = 8, CFI_HEADER_SIZE = 8 };
80  enum CFIVersion {
81  CFI_VERSION1 = 1, // DWARF v.2
82  CFI_VERSION3 = 3, // DWARF v.3
83  CFI_VERSION4 = 4 // DWARF v.4, v.5
84  };
85 
86  struct CIE {
88  uint8_t version;
89  char augmentation[CFI_AUG_MAX_SIZE]; // This is typically empty or very
90  // short.
91  uint8_t address_size = sizeof(uint32_t); // The size of a target address.
92  uint8_t segment_size = 0; // The size of a segment selector.
93 
95  int32_t data_align;
97  dw_offset_t inst_offset; // offset of CIE instructions in mCFIData
98  uint32_t inst_length; // length of CIE instructions in mCFIData
99  uint8_t ptr_encoding;
100  uint8_t lsda_addr_encoding; // The encoding of the LSDA address in the FDE
101  // augmentation data
102  lldb::addr_t personality_loc; // (file) address of the pointer to the
103  // personality routine
105 
106  CIE(dw_offset_t offset)
107  : cie_offset(offset), version(-1), code_align(0), data_align(0),
109  inst_length(0), ptr_encoding(0),
110  lsda_addr_encoding(llvm::dwarf::DW_EH_PE_omit),
112  };
113 
114  typedef std::shared_ptr<CIE> CIESP;
115 
116  typedef std::map<dw_offset_t, CIESP> cie_map_t;
117 
118  // Start address (file address), size, offset of FDE location used for
119  // finding an FDE for a given File address; the start address field is an
120  // offset into an individual Module.
122 
123  bool IsEHFrame() const;
124 
125  llvm::Optional<FDEEntryMap::Entry>
127 
128  void GetFDEIndex();
129 
130  bool FDEToUnwindPlan(uint32_t offset, Address startaddr,
131  UnwindPlan &unwind_plan);
132 
133  const CIE *GetCIE(dw_offset_t cie_offset);
134 
135  void GetCFIData();
136 
137  // Applies the specified DWARF opcode to the given row. This function handle
138  // the commands operates only on a single row (these are the ones what can
139  // appear both in
140  // CIE and in FDE).
141  // Returns true if the opcode is handled and false otherwise.
142  bool HandleCommonDwarfOpcode(uint8_t primary_opcode, uint8_t extended_opcode,
143  int32_t data_align, lldb::offset_t &offset,
144  UnwindPlan::Row &row);
145 
147  lldb::SectionSP m_section_sp;
150 
152  bool m_cfi_data_initialized = false; // only copy the section into the DE once
153 
155  bool m_fde_index_initialized = false; // only scan the section for FDEs once
156  std::mutex m_fde_index_mutex; // and isolate the thread that does it
157 
159 
160  CIESP
161  ParseCIE(const uint32_t cie_offset);
162 
165  }
166 };
167 
168 } // namespace lldb_private
169 
170 #endif // LLDB_SYMBOL_DWARFCALLFRAMEINFO_H
lldb_private::DWARFCallFrameInfo::m_fde_index_mutex
std::mutex m_fde_index_mutex
Definition: DWARFCallFrameInfo.h:156
lldb_private::DWARFCallFrameInfo::CFI_VERSION1
@ CFI_VERSION1
Definition: DWARFCallFrameInfo.h:81
llvm
Definition: Debugger.h:50
lldb_private::DWARFCallFrameInfo::CIE::data_align
int32_t data_align
Definition: DWARFCallFrameInfo.h:95
LLDB_INVALID_REGNUM
#define LLDB_INVALID_REGNUM
Definition: lldb-defines.h:79
lldb_private::DWARFCallFrameInfo::m_fde_index
FDEEntryMap m_fde_index
Definition: DWARFCallFrameInfo.h:154
lldb::eRegisterKindDWARF
@ eRegisterKindDWARF
the register numbers seen DWARF
Definition: lldb-enumerations.h:229
lldb_private::DWARFCallFrameInfo::GetFDEIndex
void GetFDEIndex()
Definition: DWARFCallFrameInfo.cpp:412
lldb_private::DWARFCallFrameInfo::CIE::inst_length
uint32_t inst_length
Definition: DWARFCallFrameInfo.h:98
lldb_private::DWARFCallFrameInfo::GetRegisterKind
lldb::RegisterKind GetRegisterKind() const
Definition: DWARFCallFrameInfo.h:163
lldb_private::DWARFCallFrameInfo::CFI_AUG_MAX_SIZE
@ CFI_AUG_MAX_SIZE
Definition: DWARFCallFrameInfo.h:79
lldb_private::Flags
Definition: Flags.h:22
lldb_private::DWARFCallFrameInfo::GetCFIData
void GetCFIData()
Definition: DWARFCallFrameInfo.cpp:399
lldb_private::DWARFCallFrameInfo::CIE::inst_offset
dw_offset_t inst_offset
Definition: DWARFCallFrameInfo.h:97
lldb::offset_t
uint64_t offset_t
Definition: lldb-types.h:87
lldb_private::DWARFCallFrameInfo::CFI_VERSION4
@ CFI_VERSION4
Definition: DWARFCallFrameInfo.h:83
lldb_private::DWARFCallFrameInfo::CIE::return_addr_reg_num
uint32_t return_addr_reg_num
Definition: DWARFCallFrameInfo.h:96
lldb::addr_t
uint64_t addr_t
Definition: lldb-types.h:83
lldb_private::DWARFCallFrameInfo::GetFirstFDEEntryInRange
llvm::Optional< FDEEntryMap::Entry > GetFirstFDEEntryInRange(const AddressRange &range)
Definition: DWARFCallFrameInfo.cpp:197
RangeMap.h
lldb_private::DWARFCallFrameInfo::CIE::cie_offset
dw_offset_t cie_offset
Definition: DWARFCallFrameInfo.h:87
lldb_private::DWARFCallFrameInfo::m_type
Type m_type
Definition: DWARFCallFrameInfo.h:158
lldb_private::DWARFCallFrameInfo::GetAddressRange
bool GetAddressRange(Address addr, AddressRange &range)
Definition: DWARFCallFrameInfo.cpp:174
lldb_private::DWARFCallFrameInfo::CIE
Definition: DWARFCallFrameInfo.h:86
lldb_private::DWARFCallFrameInfo::CIE::lsda_addr_encoding
uint8_t lsda_addr_encoding
Definition: DWARFCallFrameInfo.h:100
lldb_private::DWARFCallFrameInfo::CFIVersion
CFIVersion
Definition: DWARFCallFrameInfo.h:80
lldb::eRegisterKindEHFrame
@ eRegisterKindEHFrame
the register numbers seen in eh_frame
Definition: lldb-enumerations.h:228
lldb_private::DWARFCallFrameInfo::m_cfi_data
DataExtractor m_cfi_data
Definition: DWARFCallFrameInfo.h:151
lldb_private::DWARFCallFrameInfo::EH
@ EH
Definition: DWARFCallFrameInfo.h:35
dwarf
Definition: ABISysV_arc.cpp:61
lldb_private::DataExtractor
Definition: DataExtractor.h:48
lldb_private::DWARFCallFrameInfo::CIE::CIE
CIE(dw_offset_t offset)
Definition: DWARFCallFrameInfo.h:106
lldb_private::DWARFCallFrameInfo::HandleCommonDwarfOpcode
bool HandleCommonDwarfOpcode(uint8_t primary_opcode, uint8_t extended_opcode, int32_t data_align, lldb::offset_t &offset, UnwindPlan::Row &row)
Definition: DWARFCallFrameInfo.cpp:817
lldb_private::DWARFCallFrameInfo::~DWARFCallFrameInfo
~DWARFCallFrameInfo()=default
lldb_private::AddressRange
Definition: AddressRange.h:25
lldb_private::DWARFCallFrameInfo::GetUnwindPlan
bool GetUnwindPlan(const Address &addr, UnwindPlan &unwind_plan)
Return an UnwindPlan based on the call frame information encoded in the FDE of this DWARFCallFrameInf...
Definition: DWARFCallFrameInfo.cpp:152
lldb_private::DWARFCallFrameInfo::CIE::initial_row
lldb_private::UnwindPlan::Row initial_row
Definition: DWARFCallFrameInfo.h:104
lldb_private::DWARFCallFrameInfo::DWARF
@ DWARF
Definition: DWARFCallFrameInfo.h:35
lldb_private::DWARFCallFrameInfo::CIE::ptr_encoding
uint8_t ptr_encoding
Definition: DWARFCallFrameInfo.h:99
lldb_private::DWARFCallFrameInfo::cie_map_t
std::map< dw_offset_t, CIESP > cie_map_t
Definition: DWARFCallFrameInfo.h:116
lldb_private::RangeVector< lldb::addr_t, uint32_t >
lldb_private::DWARFCallFrameInfo::IsEHFrame
bool IsEHFrame() const
UnwindPlan.h
lldb_private::DWARFCallFrameInfo::Type
Type
Definition: DWARFCallFrameInfo.h:35
ObjectFile.h
lldb::RegisterKind
RegisterKind
Register numbering types.
Definition: lldb-enumerations.h:227
lldb-private.h
lldb_private::DWARFCallFrameInfo::CIESP
std::shared_ptr< CIE > CIESP
Definition: DWARFCallFrameInfo.h:114
lldb_private::DWARFCallFrameInfo::CIE::code_align
uint32_t code_align
Definition: DWARFCallFrameInfo.h:94
lldb_private::DWARFCallFrameInfo::CFI_VERSION3
@ CFI_VERSION3
Definition: DWARFCallFrameInfo.h:82
dwarf.h
lldb_private::UnwindPlan::Row
Definition: UnwindPlan.h:55
uint32_t
lldb_private::DWARFCallFrameInfo::CIE::augmentation
char augmentation[CFI_AUG_MAX_SIZE]
Definition: DWARFCallFrameInfo.h:89
lldb_private::DWARFCallFrameInfo::FDEEntryMap
RangeDataVector< lldb::addr_t, uint32_t, dw_offset_t > FDEEntryMap
Definition: DWARFCallFrameInfo.h:121
lldb_private::Address
Definition: Address.h:59
lldb_private::DWARFCallFrameInfo::m_cie_map
cie_map_t m_cie_map
Definition: DWARFCallFrameInfo.h:149
lldb_private::DWARFCallFrameInfo::CIE::address_size
uint8_t address_size
Definition: DWARFCallFrameInfo.h:91
VMRange.h
lldb_private::DWARFCallFrameInfo::m_section_sp
lldb::SectionSP m_section_sp
Definition: DWARFCallFrameInfo.h:147
lldb_private::RangeDataVector< lldb::addr_t, uint32_t, dw_offset_t >
lldb_private::Type
Definition: Type.h:66
lldb_private::DWARFCallFrameInfo::FunctionAddressAndSizeVector
RangeVector< lldb::addr_t, uint32_t > FunctionAddressAndSizeVector
Definition: DWARFCallFrameInfo.h:56
lldb_private::DWARFCallFrameInfo::m_objfile
ObjectFile & m_objfile
Definition: DWARFCallFrameInfo.h:146
lldb_private::DWARFCallFrameInfo::ForEachFDEEntries
void ForEachFDEEntries(const std::function< bool(lldb::addr_t, uint32_t, dw_offset_t)> &callback)
Definition: DWARFCallFrameInfo.cpp:1015
LLDB_INVALID_ADDRESS
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:74
lldb_private::DWARFCallFrameInfo
Definition: DWARFCallFrameInfo.h:33
lldb_private::DWARFCallFrameInfo::DWARFCallFrameInfo
DWARFCallFrameInfo(ObjectFile &objfile, lldb::SectionSP &section, Type type)
Definition: DWARFCallFrameInfo.cpp:148
lldb_private::DWARFCallFrameInfo::CIE::segment_size
uint8_t segment_size
Definition: DWARFCallFrameInfo.h:92
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
Flags.h
lldb_private::DWARFCallFrameInfo::ParseCIE
CIESP ParseCIE(const uint32_t cie_offset)
Definition: DWARFCallFrameInfo.cpp:246
lldb_private::DWARFCallFrameInfo::GetCIE
const CIE * GetCIE(dw_offset_t cie_offset)
Definition: DWARFCallFrameInfo.cpp:232
lldb_private::DWARFCallFrameInfo::CIE::version
uint8_t version
Definition: DWARFCallFrameInfo.h:88
lldb_private::DWARFCallFrameInfo::FDEToUnwindPlan
bool FDEToUnwindPlan(uint32_t offset, Address startaddr, UnwindPlan &unwind_plan)
Definition: DWARFCallFrameInfo.cpp:527
lldb_private::DWARFCallFrameInfo::m_flags
Flags m_flags
Definition: DWARFCallFrameInfo.h:148
AddressRange.h
lldb_private::UnwindPlan
Definition: UnwindPlan.h:53
lldb_private::DWARFCallFrameInfo::CFI_HEADER_SIZE
@ CFI_HEADER_SIZE
Definition: DWARFCallFrameInfo.h:79
lldb_private::ObjectFile
Definition: ObjectFile.h:60
lldb_private::DWARFCallFrameInfo::CIE::personality_loc
lldb::addr_t personality_loc
Definition: DWARFCallFrameInfo.h:102
lldb_private::DWARFCallFrameInfo::GetFunctionAddressAndSizeVector
void GetFunctionAddressAndSizeVector(FunctionAddressAndSizeVector &function_info)
Definition: DWARFCallFrameInfo.cpp:213
lldb_private::DWARFCallFrameInfo::m_cfi_data_initialized
bool m_cfi_data_initialized
Definition: DWARFCallFrameInfo.h:152
lldb_private::DWARFCallFrameInfo::m_fde_index_initialized
bool m_fde_index_initialized
Definition: DWARFCallFrameInfo.h:155