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
17
18using namespace llvm;
19using namespace gsym;
20
21GsymReaderV1::GsymReaderV1(std::unique_ptr<MemoryBuffer> Buffer,
23 : GsymReader(std::move(Buffer), Endian) {}
24
26 if (auto Err = parseHeader(Hdr, SwappedHdr))
27 return Err;
28
29 // Compute section offsets from the fixed V1 layout and populate the
30 // GlobalDataSections map. V1 sections are laid out sequentially:
31 // [Header] [AddrOffsets] [AddrInfoOffsets] [FileTable] ... [StringTable]
32 const StringRef Buf = MemBuffer->getBuffer();
33 const uint64_t NumAddrs = Hdr->NumAddresses;
34 const uint8_t AddrOffSize = Hdr->AddrOffSize;
35
36 // AddrOffsets
37 uint64_t Offset = alignTo(sizeof(Header), AddrOffSize);
38 uint64_t AddrOffsetsSize = NumAddrs * AddrOffSize;
40 GlobalInfoType::AddrOffsets, Offset, AddrOffsetsSize};
41 Offset += AddrOffsetsSize;
42
43 // AddrInfoOffsets
44 Offset = alignTo(Offset, 4);
45 uint64_t AddrInfoOffsetsSize = NumAddrs * Header::getAddressInfoOffsetSize();
47 GlobalInfoType::AddrInfoOffsets, Offset, AddrInfoOffsetsSize};
48 Offset += AddrInfoOffsetsSize;
49
50 // FileTable: read NumFiles to compute the size.
52 uint64_t FTOffset = Offset;
53 uint32_t NumFiles = Data.getU32(&FTOffset);
54 uint64_t FileTableSize =
55 4 + static_cast<uint64_t>(NumFiles) *
58 Offset, FileTableSize};
59
60 // StringTable: offset and size are in the header.
62 GlobalInfoType::StringTable, Hdr->StrtabOffset, Hdr->StrtabSize};
63
64 // FunctionInfo: starts after the string table and extends to end of file.
65 const uint64_t FIOffset = Hdr->StrtabOffset + Hdr->StrtabSize;
67 GlobalInfoType::FunctionInfo, FIOffset, Buf.size() - FIOffset};
68
69 return Error::success();
70}
71
73 OS << *Hdr << "\n";
74 OS << "Address Table:\n";
75 OS << "INDEX OFFSET";
76
77 switch (getAddressOffsetSize()) {
78 case 1:
79 OS << "8 ";
80 break;
81 case 2:
82 OS << "16";
83 break;
84 case 4:
85 OS << "32";
86 break;
87 case 8:
88 OS << "64";
89 break;
90 default:
91 OS << "??";
92 break;
93 }
94 OS << " (ADDRESS)\n";
95 OS << "====== =============================== \n";
96 for (uint32_t I = 0; I < getNumAddresses(); ++I) {
97 OS << format("[%4u] ", I);
98 switch (getAddressOffsetSize()) {
99 case 1:
100 OS << HEX8(getAddrOffsets<uint8_t>()[I]);
101 break;
102 case 2:
103 OS << HEX16(getAddrOffsets<uint16_t>()[I]);
104 break;
105 case 4:
106 OS << HEX32(getAddrOffsets<uint32_t>()[I]);
107 break;
108 case 8:
109 OS << HEX32(getAddrOffsets<uint64_t>()[I]);
110 break;
111 default:
112 break;
113 }
114 OS << " (" << HEX64(*getAddress(I)) << ")\n";
115 }
116 OS << "\nAddress Info Offsets:\n";
117 OS << "INDEX Offset\n";
118 OS << "====== ==========\n";
119 for (uint32_t I = 0; I < getNumAddresses(); ++I)
120 OS << format("[%4u] ", I) << HEX32(*getAddressInfoOffset(I)) << "\n";
121 OS << "\nFiles:\n";
122 OS << "INDEX DIRECTORY BASENAME PATH\n";
123 OS << "====== ========== ========== ==============================\n";
124 for (uint32_t I = 0;; ++I) {
125 auto FE = getFile(I);
126 if (!FE)
127 break;
128 OS << format("[%4u] ", I) << HEX32(FE->Dir) << ' ' << HEX32(FE->Base)
129 << ' ';
130 dump(OS, FE);
131 OS << "\n";
132 }
133 OS << "\n";
134 gsym::dump(OS, StrTab, 4);
135 OS << "\n";
136
137 for (uint32_t I = 0; I < getNumAddresses(); ++I) {
138 OS << "FunctionInfo @ " << HEX32(*getAddressInfoOffset(I)) << ": ";
139 if (auto FI = getFunctionInfoAtIndex(I))
140 dump(OS, *FI);
141 else
142 logAllUnhandledErrors(FI.takeError(), OS, "FunctionInfo:");
143 }
144}
#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
constexpr uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition Alignment.h:144
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition Format.h:129
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