LLVM 23.0.0git
GsymReaderV1.cpp
Go to the documentation of this file.
1//===- GsymReaderV1.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
10
11#include <assert.h>
12#include <inttypes.h>
13
18
19using namespace llvm;
20using namespace gsym;
21
22GsymReaderV1::GsymReaderV1(std::unique_ptr<MemoryBuffer> Buffer,
24 : GsymReader(std::move(Buffer), Endian) {}
25
27 if (auto Err = parseHeader(Hdr, SwappedHdr))
28 return Err;
29
30 // Compute section offsets from the fixed V1 layout and populate the
31 // GlobalDataSections map. V1 sections are laid out sequentially:
32 // [Header] [AddrOffsets] [AddrInfoOffsets] [FileTable] ... [StringTable]
33 const StringRef Buf = MemBuffer->getBuffer();
34 const uint64_t NumAddrs = Hdr->NumAddresses;
35 const uint8_t AddrOffSize = Hdr->AddrOffSize;
36
37 // AddrOffsets
38 uint64_t Offset = alignTo(sizeof(Header), AddrOffSize);
39 uint64_t AddrOffsetsSize = NumAddrs * AddrOffSize;
41 GlobalInfoType::AddrOffsets, Offset, AddrOffsetsSize};
42 Offset += AddrOffsetsSize;
43
44 // AddrInfoOffsets
45 Offset = alignTo(Offset, 4);
46 uint64_t AddrInfoOffsetsSize = NumAddrs * Header::getAddressInfoOffsetSize();
48 GlobalInfoType::AddrInfoOffsets, Offset, AddrInfoOffsetsSize};
49 Offset += AddrInfoOffsetsSize;
50
51 // FileTable: read NumFiles to compute the size.
53 uint64_t FTOffset = Offset;
54 uint32_t NumFiles = Data.getU32(&FTOffset);
55 uint64_t FileTableSize =
56 4 + static_cast<uint64_t>(NumFiles) *
59 Offset, FileTableSize};
60
61 // StringTable: offset and size are in the header.
63 GlobalInfoType::StringTable, Hdr->StrtabOffset, Hdr->StrtabSize};
64
65 // FunctionInfo: starts after the string table and extends to end of file.
66 const uint64_t FIOffset = Hdr->StrtabOffset + Hdr->StrtabSize;
68 GlobalInfoType::FunctionInfo, FIOffset, Buf.size() - FIOffset};
69
70 return Error::success();
71}
72
74 OS << *Hdr << "\n";
75 OS << "Address Table:\n";
76 OS << "INDEX OFFSET";
77
78 switch (getAddressOffsetSize()) {
79 case 1:
80 OS << "8 ";
81 break;
82 case 2:
83 OS << "16";
84 break;
85 case 4:
86 OS << "32";
87 break;
88 case 8:
89 OS << "64";
90 break;
91 default:
92 OS << "??";
93 break;
94 }
95 OS << " (ADDRESS)\n";
96 OS << "====== =============================== \n";
97 for (uint32_t I = 0; I < getNumAddresses(); ++I) {
98 OS << formatv("[{0,4}] ", I);
99 switch (getAddressOffsetSize()) {
100 case 1:
101 OS << HEX8(getAddrOffsets<uint8_t>()[I]);
102 break;
103 case 2:
104 OS << HEX16(getAddrOffsets<uint16_t>()[I]);
105 break;
106 case 4:
107 OS << HEX32(getAddrOffsets<uint32_t>()[I]);
108 break;
109 case 8:
110 OS << HEX32(getAddrOffsets<uint64_t>()[I]);
111 break;
112 default:
113 break;
114 }
115 OS << " (" << HEX64(*getAddress(I)) << ")\n";
116 }
117 OS << "\nAddress Info Offsets:\n";
118 OS << "INDEX Offset\n";
119 OS << "====== ==========\n";
120 for (uint32_t I = 0; I < getNumAddresses(); ++I)
121 OS << formatv("[{0,4}] ", I) << HEX32(*getAddressInfoOffset(I)) << "\n";
122 OS << "\nFiles:\n";
123 OS << "INDEX DIRECTORY BASENAME PATH\n";
124 OS << "====== ========== ========== ==============================\n";
125 for (uint32_t I = 0;; ++I) {
126 auto FE = getFile(I);
127 if (!FE)
128 break;
129 OS << formatv("[{0,4}] ", I) << HEX32(FE->Dir) << ' ' << HEX32(FE->Base)
130 << ' ';
131 dump(OS, FE);
132 OS << "\n";
133 }
134 OS << "\n";
135 gsym::dump(OS, StrTab, 4);
136 OS << "\n";
137
138 for (uint32_t I = 0; I < getNumAddresses(); ++I) {
139 OS << "FunctionInfo @ " << HEX32(*getAddressInfoOffset(I)) << ": ";
140 if (auto FI = getFunctionInfoAtIndex(I))
141 dump(OS, *FI);
142 else
143 logAllUnhandledErrors(FI.takeError(), OS, "FunctionInfo:");
144 }
145}
#define HEX64(v)
#define HEX32(v)
#define I(x, y, z)
Definition MD5.cpp:57
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
constexpr size_t size() const
size - Get the string size.
Definition StringRef.h:143
A DataExtractor subclass that adds GSYM-specific string offset support.
uint8_t getAddressOffsetSize() const override
Get the address offset byte size for this GSYM file.
llvm::Error parseHeaderAndGlobalDataEntries() override
Parse the version-specific header and populate GlobalDataSections.
uint64_t getNumAddresses() const override
Get the number of addresses in this GSYM file.
LLVM_ABI void dump(raw_ostream &OS) override
Dump the entire Gsym data contained in this object.
GsymReaderV1(std::unique_ptr< MemoryBuffer > Buffer, llvm::endianness Endian)
std::optional< FileEntry > getFile(uint32_t Index) const
Get the a file entry for the suppplied file index.
Definition GsymReader.h:184
bool isLittleEndian() const
Definition GsymReader.h:67
llvm::Error parseHeader(const HeaderT *&OutHdr, std::unique_ptr< HeaderT > &OutSwappedHdr)
Parse and validate the header from the beginning of the memory buffer.
Definition GsymReader.h:336
LLVM_ABI std::optional< uint64_t > getAddress(size_t Index) const
Gets an address from the address table.
LLVM_ABI std::optional< uint64_t > getAddressInfoOffset(size_t Index) const
Given an address index, get the offset for the FunctionInfo.
std::map< GlobalInfoType, GlobalData > GlobalDataSections
Parsed GlobalData entries, keyed by type.
Definition GsymReader.h:54
std::unique_ptr< MemoryBuffer > MemBuffer
Definition GsymReader.h:50
LLVM_ABI llvm::Expected< FunctionInfo > getFunctionInfoAtIndex(uint64_t AddrIdx) const
Get the full function info given an address index.
llvm::endianness Endian
Definition GsymReader.h:51
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
void dump(raw_ostream &OS, const StringTable &S, uint8_t StringOffsetSize)
Definition StringTable.h:37
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:532
LLVM_ABI void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner={})
Log all errors (if any) in E to OS.
Definition Error.cpp:61
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
constexpr uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition Alignment.h:144
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:221
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1917
endianness
Definition bit.h:71
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:870
static constexpr uint64_t getEncodedSize(uint8_t StringOffsetSize)
Returns the on-disk encoded size of a FileEntry for the given string offset size.
Definition FileEntry.h:38
The GSYM header.
Definition Header.h:45
static constexpr uint8_t getStringOffsetSize()
Return the size in bytes of string table offsets.
Definition Header.h:98
static constexpr uint8_t getAddressInfoOffsetSize()
Return the size in bytes of address info offsets.
Definition Header.h:95