LLVM 23.0.0git
HeaderV2.cpp
Go to the documentation of this file.
1//===- HeaderV2.cpp ---------------------------------------------*- 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
12#include "llvm/Support/Format.h"
14
15#define HEX8(v) llvm::format_hex(v, 4)
16#define HEX16(v) llvm::format_hex(v, 6)
17#define HEX32(v) llvm::format_hex(v, 10)
18#define HEX64(v) llvm::format_hex(v, 18)
19
20using namespace llvm;
21using namespace gsym;
22
24 OS << "Header:\n";
25 OS << " Magic = " << HEX32(H.Magic) << "\n";
26 OS << " Version = " << HEX16(H.Version) << '\n';
27 OS << " AddrOffSize = " << HEX8(H.AddrOffSize) << '\n';
28 OS << " StrTableEnc = " << HEX8(static_cast<uint8_t>(H.StrTableEncoding))
29 << '\n';
30 OS << " BaseAddress = " << HEX64(H.BaseAddress) << '\n';
31 OS << " NumAddresses = " << HEX32(H.NumAddresses) << '\n';
32 return OS;
33}
34
36 if (Magic != GSYM_MAGIC)
37 return createStringError(std::errc::invalid_argument,
38 "invalid GSYM magic 0x%8.8x", Magic);
40 return createStringError(std::errc::invalid_argument,
41 "unsupported GSYM version %u", Version);
43 return createStringError(std::errc::invalid_argument,
44 "invalid address offset size %u", AddrOffSize);
45 uint8_t Encoding = static_cast<uint8_t>(StrTableEncoding);
46 switch (Encoding) {
47 case static_cast<uint8_t>(StringTableEncoding::Default):
48 break;
49 default:
50 return createStringError(std::errc::invalid_argument,
51 "unsupported string table encoding %u", Encoding);
52 }
53 return Error::success();
54}
55
57 uint64_t Offset = 0;
58 // The fixed portion of the HeaderV2 is 20 bytes:
59 // Magic(4) + Version(2) + AddrOffSize(1) + StrTableEncoding(1) +
60 // BaseAddress(8) + NumAddresses(4)
61 const uint64_t FixedHeaderSize = HeaderV2::getEncodedSize();
62 if (!Data.isValidOffsetForDataOfSize(Offset, FixedHeaderSize))
63 return createStringError(std::errc::invalid_argument,
64 "not enough data for a gsym::HeaderV2");
65 HeaderV2 H;
66 H.Magic = Data.getU32(&Offset);
67 H.Version = Data.getU16(&Offset);
68 H.AddrOffSize = Data.getU8(&Offset);
69 H.StrTableEncoding = static_cast<StringTableEncoding>(Data.getU8(&Offset));
70 H.BaseAddress = Data.getU64(&Offset);
71 H.NumAddresses = Data.getU32(&Offset);
72 if (llvm::Error Err = H.checkForError())
73 return std::move(Err);
74 return H;
75}
76
78 if (llvm::Error Err = checkForError())
79 return Err;
80 O.writeU32(Magic);
81 O.writeU16(Version);
82 O.writeU8(AddrOffSize);
83 O.writeU8(static_cast<uint8_t>(StrTableEncoding));
84 O.writeU64(BaseAddress);
85 O.writeU32(NumAddresses);
86 return Error::success();
87}
88
89bool llvm::gsym::operator==(const HeaderV2 &LHS, const HeaderV2 &RHS) {
90 return LHS.Magic == RHS.Magic && LHS.Version == RHS.Version &&
91 LHS.AddrOffSize == RHS.AddrOffSize &&
92 LHS.StrTableEncoding == RHS.StrTableEncoding &&
93 LHS.BaseAddress == RHS.BaseAddress &&
94 LHS.NumAddresses == RHS.NumAddresses;
95}
#define HEX8(v)
Definition HeaderV2.cpp:15
#define HEX16(v)
Definition HeaderV2.cpp:16
#define HEX64(v)
Definition HeaderV2.cpp:18
#define HEX32(v)
Definition HeaderV2.cpp:17
#define H(x, y, z)
Definition MD5.cpp:56
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
Tagged union holding either a T or a Error.
Definition Error.h:485
A simplified binary data writer class that doesn't require targets, target definitions,...
Definition FileWriter.h:30
A DataExtractor subclass that adds GSYM-specific string offset support.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
LLVM_ABI raw_ostream & operator<<(raw_ostream &OS, const CallSiteInfo &CSI)
bool operator==(const FunctionInfo &LHS, const FunctionInfo &RHS)
constexpr uint32_t GSYM_MAGIC
Definition Header.h:25
StringTableEncoding
Encoding format for the string table.
Definition HeaderV2.h:27
@ Default
A list of NULL-terminated strings (same as V1).
Definition HeaderV2.h:30
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:532
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition Error.h:1321
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:221
The GSYM V2 header.
Definition HeaderV2.h:60
static constexpr uint32_t getVersion()
Return the version of this header.
Definition HeaderV2.h:83
static constexpr uint64_t getEncodedSize()
Return the on-disk encoded size of the header in bytes.
Definition HeaderV2.h:87
uint64_t BaseAddress
The 64 bit base address that all address offsets in the address offsets table are relative to.
Definition HeaderV2.h:77
uint32_t Magic
The magic bytes should be set to GSYM_MAGIC.
Definition HeaderV2.h:64
static LLVM_ABI llvm::Expected< HeaderV2 > decode(GsymDataExtractor &Data)
Decode an object from a binary data stream.
Definition HeaderV2.cpp:56
uint32_t NumAddresses
The number of addresses stored in the address offsets table and the address info offsets table.
Definition HeaderV2.h:80
uint16_t Version
The version number determines how the header is decoded.
Definition HeaderV2.h:69
StringTableEncoding StrTableEncoding
String table encoding. Allows for future encoding for string table.
Definition HeaderV2.h:73
uint8_t AddrOffSize
The size in bytes of each address offset in the address offsets table.
Definition HeaderV2.h:71
LLVM_ABI llvm::Error checkForError() const
Check if a header is valid and return an error if anything is wrong.
Definition HeaderV2.cpp:35
LLVM_ABI llvm::Error encode(FileWriter &O) const
Encode this object into FileWriter stream.
Definition HeaderV2.cpp:77