1 //===- llvm/unittest/DebugInfo/GSYMTest.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 9 #include "llvm/ADT/DenseMap.h" 10 #include "llvm/ADT/SmallString.h" 11 #include "llvm/DebugInfo/DWARF/DWARFContext.h" 12 #include "llvm/DebugInfo/GSYM/DwarfTransformer.h" 13 #include "llvm/DebugInfo/GSYM/ExtractRanges.h" 14 #include "llvm/DebugInfo/GSYM/FileEntry.h" 15 #include "llvm/DebugInfo/GSYM/FileWriter.h" 16 #include "llvm/DebugInfo/GSYM/FunctionInfo.h" 17 #include "llvm/DebugInfo/GSYM/GsymCreator.h" 18 #include "llvm/DebugInfo/GSYM/GsymReader.h" 19 #include "llvm/DebugInfo/GSYM/Header.h" 20 #include "llvm/DebugInfo/GSYM/InlineInfo.h" 21 #include "llvm/DebugInfo/GSYM/StringTable.h" 22 #include "llvm/ObjectYAML/DWARFEmitter.h" 23 #include "llvm/Support/DataExtractor.h" 24 #include "llvm/Support/Endian.h" 25 #include "llvm/Testing/Support/Error.h" 26 27 #include "gtest/gtest.h" 28 #include "gmock/gmock.h" 29 #include <string> 30 31 using namespace llvm; 32 using namespace gsym; 33 34 void checkError(ArrayRef<std::string> ExpectedMsgs, Error Err) { 35 ASSERT_TRUE(bool(Err)); 36 size_t WhichMsg = 0; 37 Error Remaining = 38 handleErrors(std::move(Err), [&](const ErrorInfoBase &Actual) { 39 ASSERT_LT(WhichMsg, ExpectedMsgs.size()); 40 // Use .str(), because googletest doesn't visualise a StringRef 41 // properly. 42 EXPECT_EQ(Actual.message(), ExpectedMsgs[WhichMsg++]); 43 }); 44 EXPECT_EQ(WhichMsg, ExpectedMsgs.size()); 45 EXPECT_FALSE(Remaining); 46 } 47 48 void checkError(std::string ExpectedMsg, Error Err) { 49 checkError(ArrayRef<std::string>{ExpectedMsg}, std::move(Err)); 50 } 51 TEST(GSYMTest, TestFileEntry) { 52 // Make sure default constructed GSYM FileEntry has zeroes in the 53 // directory and basename string table indexes. 54 FileEntry empty1; 55 FileEntry empty2; 56 EXPECT_EQ(empty1.Dir, 0u); 57 EXPECT_EQ(empty1.Base, 0u); 58 // Verify equality operator works 59 FileEntry a1(10, 30); 60 FileEntry a2(10, 30); 61 FileEntry b(10, 40); 62 EXPECT_EQ(empty1, empty2); 63 EXPECT_EQ(a1, a2); 64 EXPECT_NE(a1, b); 65 EXPECT_NE(a1, empty1); 66 // Test we can use llvm::gsym::FileEntry in llvm::DenseMap. 67 DenseMap<FileEntry, uint32_t> EntryToIndex; 68 constexpr uint32_t Index1 = 1; 69 constexpr uint32_t Index2 = 1; 70 auto R = EntryToIndex.insert(std::make_pair(a1, Index1)); 71 EXPECT_TRUE(R.second); 72 EXPECT_EQ(R.first->second, Index1); 73 R = EntryToIndex.insert(std::make_pair(a1, Index1)); 74 EXPECT_FALSE(R.second); 75 EXPECT_EQ(R.first->second, Index1); 76 R = EntryToIndex.insert(std::make_pair(b, Index2)); 77 EXPECT_TRUE(R.second); 78 EXPECT_EQ(R.first->second, Index2); 79 R = EntryToIndex.insert(std::make_pair(a1, Index2)); 80 EXPECT_FALSE(R.second); 81 EXPECT_EQ(R.first->second, Index2); 82 } 83 84 TEST(GSYMTest, TestFunctionInfo) { 85 // Test GSYM FunctionInfo structs and functionality. 86 FunctionInfo invalid; 87 EXPECT_FALSE(invalid.isValid()); 88 EXPECT_FALSE(invalid.hasRichInfo()); 89 const uint64_t StartAddr = 0x1000; 90 const uint64_t EndAddr = 0x1100; 91 const uint64_t Size = EndAddr - StartAddr; 92 const uint32_t NameOffset = 30; 93 FunctionInfo FI(StartAddr, Size, NameOffset); 94 EXPECT_TRUE(FI.isValid()); 95 EXPECT_FALSE(FI.hasRichInfo()); 96 EXPECT_EQ(FI.startAddress(), StartAddr); 97 EXPECT_EQ(FI.endAddress(), EndAddr); 98 EXPECT_EQ(FI.size(), Size); 99 const uint32_t FileIdx = 1; 100 const uint32_t Line = 12; 101 FI.OptLineTable = LineTable(); 102 FI.OptLineTable->push(LineEntry(StartAddr,FileIdx,Line)); 103 EXPECT_TRUE(FI.hasRichInfo()); 104 FI.clear(); 105 EXPECT_FALSE(FI.isValid()); 106 EXPECT_FALSE(FI.hasRichInfo()); 107 108 FunctionInfo A1(0x1000, 0x100, NameOffset); 109 FunctionInfo A2(0x1000, 0x100, NameOffset); 110 FunctionInfo B; 111 // Check == operator 112 EXPECT_EQ(A1, A2); 113 // Make sure things are not equal if they only differ by start address. 114 B = A2; 115 B.Range = {0x1001, B.endAddress()}; 116 EXPECT_NE(B, A2); 117 // Make sure things are not equal if they only differ by size. 118 B = A2; 119 B.Range = {B.startAddress(), B.startAddress() + 0x101}; 120 EXPECT_NE(B, A2); 121 // Make sure things are not equal if they only differ by name. 122 B = A2; 123 B.Name = 60; 124 EXPECT_NE(B, A2); 125 // Check < operator. 126 // Check less than where address differs. 127 B = A2; 128 B.Range = {A2.startAddress() + 0x1000, A2.endAddress() + 0x1000}; 129 EXPECT_LT(A1, B); 130 131 // We use the < operator to take a variety of different FunctionInfo 132 // structs from a variety of sources: symtab, debug info, runtime info 133 // and we sort them and want the sorting to allow us to quickly get the 134 // best version of a function info. 135 FunctionInfo FISymtab(StartAddr, Size, NameOffset); 136 FunctionInfo FIWithLines(StartAddr, Size, NameOffset); 137 FIWithLines.OptLineTable = LineTable(); 138 FIWithLines.OptLineTable->push(LineEntry(StartAddr,FileIdx,Line)); 139 // Test that a FunctionInfo with just a name and size is less than one 140 // that has name, size and any number of line table entries 141 EXPECT_LT(FISymtab, FIWithLines); 142 143 FunctionInfo FIWithLinesAndInline = FIWithLines; 144 FIWithLinesAndInline.Inline = InlineInfo(); 145 FIWithLinesAndInline.Inline->Ranges.insert( 146 AddressRange(StartAddr, StartAddr + 0x10)); 147 // Test that a FunctionInfo with name, size, and line entries is less than 148 // the same one with valid inline info 149 EXPECT_LT(FIWithLines, FIWithLinesAndInline); 150 151 // Test if we have an entry with lines and one with more lines for the same 152 // range, the ones with more lines is greater than the one with less. 153 FunctionInfo FIWithMoreLines = FIWithLines; 154 FIWithMoreLines.OptLineTable->push(LineEntry(StartAddr,FileIdx,Line+5)); 155 EXPECT_LT(FIWithLines, FIWithMoreLines); 156 157 // Test that if we have the same number of lines we compare the line entries 158 // in the FunctionInfo.OptLineTable.Lines vector. 159 FunctionInfo FIWithLinesWithHigherAddress = FIWithLines; 160 FIWithLinesWithHigherAddress.OptLineTable->get(0).Addr += 0x10; 161 EXPECT_LT(FIWithLines, FIWithLinesWithHigherAddress); 162 } 163 164 static void TestFunctionInfoDecodeError(llvm::support::endianness ByteOrder, 165 StringRef Bytes, 166 const uint64_t BaseAddr, 167 std::string ExpectedErrorMsg) { 168 uint8_t AddressSize = 4; 169 DataExtractor Data(Bytes, ByteOrder == llvm::support::little, AddressSize); 170 llvm::Expected<FunctionInfo> Decoded = FunctionInfo::decode(Data, BaseAddr); 171 // Make sure decoding fails. 172 ASSERT_FALSE((bool)Decoded); 173 // Make sure decoded object is the same as the one we encoded. 174 checkError(ExpectedErrorMsg, Decoded.takeError()); 175 } 176 177 TEST(GSYMTest, TestFunctionInfoDecodeErrors) { 178 // Test decoding FunctionInfo objects that ensure we report an appropriate 179 // error message. 180 const llvm::support::endianness ByteOrder = llvm::support::little; 181 SmallString<512> Str; 182 raw_svector_ostream OutStrm(Str); 183 FileWriter FW(OutStrm, ByteOrder); 184 const uint64_t BaseAddr = 0x100; 185 TestFunctionInfoDecodeError(ByteOrder, OutStrm.str(), BaseAddr, 186 "0x00000000: missing FunctionInfo Size"); 187 FW.writeU32(0x100); // Function size. 188 TestFunctionInfoDecodeError(ByteOrder, OutStrm.str(), BaseAddr, 189 "0x00000004: missing FunctionInfo Name"); 190 // Write out an invalid Name string table offset of zero. 191 FW.writeU32(0); 192 TestFunctionInfoDecodeError(ByteOrder, OutStrm.str(), BaseAddr, 193 "0x00000004: invalid FunctionInfo Name value 0x00000000"); 194 // Modify the Name to be 0x00000001, which is a valid value. 195 FW.fixup32(0x00000001, 4); 196 TestFunctionInfoDecodeError(ByteOrder, OutStrm.str(), BaseAddr, 197 "0x00000008: missing FunctionInfo InfoType value"); 198 auto FixupOffset = FW.tell(); 199 FW.writeU32(1); // InfoType::LineTableInfo. 200 TestFunctionInfoDecodeError(ByteOrder, OutStrm.str(), BaseAddr, 201 "0x0000000c: missing FunctionInfo InfoType length"); 202 FW.fixup32(4, FixupOffset); // Write an invalid InfoType enumeration value 203 FW.writeU32(0); // LineTableInfo InfoType data length. 204 TestFunctionInfoDecodeError(ByteOrder, OutStrm.str(), BaseAddr, 205 "0x00000008: unsupported InfoType 4"); 206 } 207 208 static void TestFunctionInfoEncodeError(llvm::support::endianness ByteOrder, 209 const FunctionInfo &FI, 210 std::string ExpectedErrorMsg) { 211 SmallString<512> Str; 212 raw_svector_ostream OutStrm(Str); 213 FileWriter FW(OutStrm, ByteOrder); 214 Expected<uint64_t> ExpectedOffset = FI.encode(FW); 215 ASSERT_FALSE(ExpectedOffset); 216 checkError(ExpectedErrorMsg, ExpectedOffset.takeError()); 217 } 218 219 TEST(GSYMTest, TestFunctionInfoEncodeErrors) { 220 const uint64_t FuncAddr = 0x1000; 221 const uint64_t FuncSize = 0x100; 222 const uint32_t InvalidName = 0; 223 const uint32_t ValidName = 1; 224 FunctionInfo InvalidNameFI(FuncAddr, FuncSize, InvalidName); 225 TestFunctionInfoEncodeError(llvm::support::little, InvalidNameFI, 226 "attempted to encode invalid FunctionInfo object"); 227 228 FunctionInfo InvalidLineTableFI(FuncAddr, FuncSize, ValidName); 229 // Empty line tables are not valid. Verify if the encoding of anything 230 // in our line table fails, that we see get the error propagated. 231 InvalidLineTableFI.OptLineTable = LineTable(); 232 TestFunctionInfoEncodeError(llvm::support::little, InvalidLineTableFI, 233 "attempted to encode invalid LineTable object"); 234 235 FunctionInfo InvalidInlineInfoFI(FuncAddr, FuncSize, ValidName); 236 // Empty line tables are not valid. Verify if the encoding of anything 237 // in our line table fails, that we see get the error propagated. 238 InvalidInlineInfoFI.Inline = InlineInfo(); 239 TestFunctionInfoEncodeError(llvm::support::little, InvalidInlineInfoFI, 240 "attempted to encode invalid InlineInfo object"); 241 } 242 243 static void TestFunctionInfoEncodeDecode(llvm::support::endianness ByteOrder, 244 const FunctionInfo &FI) { 245 // Test encoding and decoding FunctionInfo objects. 246 SmallString<512> Str; 247 raw_svector_ostream OutStrm(Str); 248 FileWriter FW(OutStrm, ByteOrder); 249 llvm::Expected<uint64_t> ExpectedOffset = FI.encode(FW); 250 ASSERT_TRUE(bool(ExpectedOffset)); 251 // Verify we got the encoded offset back from the encode function. 252 ASSERT_EQ(ExpectedOffset.get(), 0ULL); 253 std::string Bytes(OutStrm.str()); 254 uint8_t AddressSize = 4; 255 DataExtractor Data(Bytes, ByteOrder == llvm::support::little, AddressSize); 256 llvm::Expected<FunctionInfo> Decoded = 257 FunctionInfo::decode(Data, FI.Range.start()); 258 // Make sure decoding succeeded. 259 ASSERT_TRUE((bool)Decoded); 260 // Make sure decoded object is the same as the one we encoded. 261 EXPECT_EQ(FI, Decoded.get()); 262 } 263 264 static void AddLines(uint64_t FuncAddr, uint32_t FileIdx, FunctionInfo &FI) { 265 FI.OptLineTable = LineTable(); 266 LineEntry Line0(FuncAddr + 0x000, FileIdx, 10); 267 LineEntry Line1(FuncAddr + 0x010, FileIdx, 11); 268 LineEntry Line2(FuncAddr + 0x100, FileIdx, 1000); 269 FI.OptLineTable->push(Line0); 270 FI.OptLineTable->push(Line1); 271 FI.OptLineTable->push(Line2); 272 } 273 274 275 static void AddInline(uint64_t FuncAddr, uint64_t FuncSize, FunctionInfo &FI) { 276 FI.Inline = InlineInfo(); 277 FI.Inline->Ranges.insert(AddressRange(FuncAddr, FuncAddr + FuncSize)); 278 InlineInfo Inline1; 279 Inline1.Ranges.insert(AddressRange(FuncAddr + 0x10, FuncAddr + 0x30)); 280 Inline1.Name = 1; 281 Inline1.CallFile = 1; 282 Inline1.CallLine = 11; 283 FI.Inline->Children.push_back(Inline1); 284 } 285 286 TEST(GSYMTest, TestFunctionInfoEncoding) { 287 constexpr uint64_t FuncAddr = 0x1000; 288 constexpr uint64_t FuncSize = 0x100; 289 constexpr uint32_t FuncName = 1; 290 constexpr uint32_t FileIdx = 1; 291 // Make sure that we can encode and decode a FunctionInfo with no line table 292 // or inline info. 293 FunctionInfo FI(FuncAddr, FuncSize, FuncName); 294 TestFunctionInfoEncodeDecode(llvm::support::little, FI); 295 TestFunctionInfoEncodeDecode(llvm::support::big, FI); 296 297 // Make sure that we can encode and decode a FunctionInfo with a line table 298 // and no inline info. 299 FunctionInfo FILines(FuncAddr, FuncSize, FuncName); 300 AddLines(FuncAddr, FileIdx, FILines); 301 TestFunctionInfoEncodeDecode(llvm::support::little, FILines); 302 TestFunctionInfoEncodeDecode(llvm::support::big, FILines); 303 304 // Make sure that we can encode and decode a FunctionInfo with no line table 305 // and with inline info. 306 FunctionInfo FIInline(FuncAddr, FuncSize, FuncName); 307 AddInline(FuncAddr, FuncSize, FIInline); 308 TestFunctionInfoEncodeDecode(llvm::support::little, FIInline); 309 TestFunctionInfoEncodeDecode(llvm::support::big, FIInline); 310 311 // Make sure that we can encode and decode a FunctionInfo with no line table 312 // and with inline info. 313 FunctionInfo FIBoth(FuncAddr, FuncSize, FuncName); 314 AddLines(FuncAddr, FileIdx, FIBoth); 315 AddInline(FuncAddr, FuncSize, FIBoth); 316 TestFunctionInfoEncodeDecode(llvm::support::little, FIBoth); 317 TestFunctionInfoEncodeDecode(llvm::support::big, FIBoth); 318 } 319 320 static void TestInlineInfoEncodeDecode(llvm::support::endianness ByteOrder, 321 const InlineInfo &Inline) { 322 // Test encoding and decoding InlineInfo objects 323 SmallString<512> Str; 324 raw_svector_ostream OutStrm(Str); 325 FileWriter FW(OutStrm, ByteOrder); 326 const uint64_t BaseAddr = Inline.Ranges[0].start(); 327 llvm::Error Err = Inline.encode(FW, BaseAddr); 328 ASSERT_FALSE(Err); 329 std::string Bytes(OutStrm.str()); 330 uint8_t AddressSize = 4; 331 DataExtractor Data(Bytes, ByteOrder == llvm::support::little, AddressSize); 332 llvm::Expected<InlineInfo> Decoded = InlineInfo::decode(Data, BaseAddr); 333 // Make sure decoding succeeded. 334 ASSERT_TRUE((bool)Decoded); 335 // Make sure decoded object is the same as the one we encoded. 336 EXPECT_EQ(Inline, Decoded.get()); 337 } 338 339 static void TestInlineInfoDecodeError(llvm::support::endianness ByteOrder, 340 StringRef Bytes, const uint64_t BaseAddr, 341 std::string ExpectedErrorMsg) { 342 uint8_t AddressSize = 4; 343 DataExtractor Data(Bytes, ByteOrder == llvm::support::little, AddressSize); 344 llvm::Expected<InlineInfo> Decoded = InlineInfo::decode(Data, BaseAddr); 345 // Make sure decoding fails. 346 ASSERT_FALSE((bool)Decoded); 347 // Make sure decoded object is the same as the one we encoded. 348 checkError(ExpectedErrorMsg, Decoded.takeError()); 349 } 350 351 static void TestInlineInfoEncodeError(llvm::support::endianness ByteOrder, 352 const InlineInfo &Inline, 353 std::string ExpectedErrorMsg) { 354 SmallString<512> Str; 355 raw_svector_ostream OutStrm(Str); 356 FileWriter FW(OutStrm, ByteOrder); 357 const uint64_t BaseAddr = 358 Inline.Ranges.empty() ? 0 : Inline.Ranges[0].start(); 359 llvm::Error Err = Inline.encode(FW, BaseAddr); 360 checkError(ExpectedErrorMsg, std::move(Err)); 361 } 362 363 TEST(GSYMTest, TestInlineInfo) { 364 // Test InlineInfo structs. 365 InlineInfo II; 366 EXPECT_FALSE(II.isValid()); 367 II.Ranges.insert(AddressRange(0x1000, 0x2000)); 368 // Make sure InlineInfo in valid with just an address range since 369 // top level InlineInfo objects have ranges with no name, call file 370 // or call line 371 EXPECT_TRUE(II.isValid()); 372 // Make sure InlineInfo isn't after being cleared. 373 II.clear(); 374 EXPECT_FALSE(II.isValid()); 375 376 // Create an InlineInfo that contains the following data. The 377 // indentation of the address range indicates the parent child 378 // relationships of the InlineInfo objects: 379 // 380 // Variable Range and values 381 // =========== ==================================================== 382 // Root [0x100-0x200) (no name, file, or line) 383 // Inline1 [0x150-0x160) Name = 1, File = 1, Line = 11 384 // Inline1Sub1 [0x152-0x155) Name = 2, File = 2, Line = 22 385 // Inline1Sub2 [0x157-0x158) Name = 3, File = 3, Line = 33 386 InlineInfo Root; 387 Root.Ranges.insert(AddressRange(0x100, 0x200)); 388 InlineInfo Inline1; 389 Inline1.Ranges.insert(AddressRange(0x150, 0x160)); 390 Inline1.Name = 1; 391 Inline1.CallFile = 1; 392 Inline1.CallLine = 11; 393 InlineInfo Inline1Sub1; 394 Inline1Sub1.Ranges.insert(AddressRange(0x152, 0x155)); 395 Inline1Sub1.Name = 2; 396 Inline1Sub1.CallFile = 2; 397 Inline1Sub1.CallLine = 22; 398 InlineInfo Inline1Sub2; 399 Inline1Sub2.Ranges.insert(AddressRange(0x157, 0x158)); 400 Inline1Sub2.Name = 3; 401 Inline1Sub2.CallFile = 3; 402 Inline1Sub2.CallLine = 33; 403 Inline1.Children.push_back(Inline1Sub1); 404 Inline1.Children.push_back(Inline1Sub2); 405 Root.Children.push_back(Inline1); 406 407 // Make sure an address that is out of range won't match 408 EXPECT_FALSE(Root.getInlineStack(0x50)); 409 410 // Verify that we get no inline stacks for addresses out of [0x100-0x200) 411 EXPECT_FALSE(Root.getInlineStack(Root.Ranges[0].start() - 1)); 412 EXPECT_FALSE(Root.getInlineStack(Root.Ranges[0].end())); 413 414 // Verify we get no inline stack entries for addresses that are in 415 // [0x100-0x200) but not in [0x150-0x160) 416 EXPECT_FALSE(Root.getInlineStack(Inline1.Ranges[0].start() - 1)); 417 EXPECT_FALSE(Root.getInlineStack(Inline1.Ranges[0].end())); 418 419 // Verify we get one inline stack entry for addresses that are in 420 // [[0x150-0x160)) but not in [0x152-0x155) or [0x157-0x158) 421 auto InlineInfos = Root.getInlineStack(Inline1.Ranges[0].start()); 422 ASSERT_TRUE(InlineInfos); 423 ASSERT_EQ(InlineInfos->size(), 1u); 424 ASSERT_EQ(*InlineInfos->at(0), Inline1); 425 InlineInfos = Root.getInlineStack(Inline1.Ranges[0].end() - 1); 426 EXPECT_TRUE(InlineInfos); 427 ASSERT_EQ(InlineInfos->size(), 1u); 428 ASSERT_EQ(*InlineInfos->at(0), Inline1); 429 430 // Verify we get two inline stack entries for addresses that are in 431 // [0x152-0x155) 432 InlineInfos = Root.getInlineStack(Inline1Sub1.Ranges[0].start()); 433 EXPECT_TRUE(InlineInfos); 434 ASSERT_EQ(InlineInfos->size(), 2u); 435 ASSERT_EQ(*InlineInfos->at(0), Inline1Sub1); 436 ASSERT_EQ(*InlineInfos->at(1), Inline1); 437 InlineInfos = Root.getInlineStack(Inline1Sub1.Ranges[0].end() - 1); 438 EXPECT_TRUE(InlineInfos); 439 ASSERT_EQ(InlineInfos->size(), 2u); 440 ASSERT_EQ(*InlineInfos->at(0), Inline1Sub1); 441 ASSERT_EQ(*InlineInfos->at(1), Inline1); 442 443 // Verify we get two inline stack entries for addresses that are in 444 // [0x157-0x158) 445 InlineInfos = Root.getInlineStack(Inline1Sub2.Ranges[0].start()); 446 EXPECT_TRUE(InlineInfos); 447 ASSERT_EQ(InlineInfos->size(), 2u); 448 ASSERT_EQ(*InlineInfos->at(0), Inline1Sub2); 449 ASSERT_EQ(*InlineInfos->at(1), Inline1); 450 InlineInfos = Root.getInlineStack(Inline1Sub2.Ranges[0].end() - 1); 451 EXPECT_TRUE(InlineInfos); 452 ASSERT_EQ(InlineInfos->size(), 2u); 453 ASSERT_EQ(*InlineInfos->at(0), Inline1Sub2); 454 ASSERT_EQ(*InlineInfos->at(1), Inline1); 455 456 // Test encoding and decoding InlineInfo objects 457 TestInlineInfoEncodeDecode(llvm::support::little, Root); 458 TestInlineInfoEncodeDecode(llvm::support::big, Root); 459 } 460 461 TEST(GSYMTest, TestInlineInfoEncodeErrors) { 462 // Test InlineInfo encoding errors. 463 464 // Test that we get an error when trying to encode an InlineInfo object 465 // that has no ranges. 466 InlineInfo Empty; 467 std::string EmptyErr("attempted to encode invalid InlineInfo object"); 468 TestInlineInfoEncodeError(llvm::support::little, Empty, EmptyErr); 469 TestInlineInfoEncodeError(llvm::support::big, Empty, EmptyErr); 470 471 // Verify that we get an error trying to encode an InlineInfo object that has 472 // a child InlineInfo that has no ranges. 473 InlineInfo ContainsEmpty; 474 ContainsEmpty.Ranges.insert({0x100, 0x200}); 475 ContainsEmpty.Children.push_back(Empty); 476 TestInlineInfoEncodeError(llvm::support::little, ContainsEmpty, EmptyErr); 477 TestInlineInfoEncodeError(llvm::support::big, ContainsEmpty, EmptyErr); 478 479 // Verify that we get an error trying to encode an InlineInfo object that has 480 // a child whose address range is not contained in the parent address range. 481 InlineInfo ChildNotContained; 482 std::string ChildNotContainedErr("child range not contained in parent"); 483 ChildNotContained.Ranges.insert({0x100, 0x200}); 484 InlineInfo ChildNotContainedChild; 485 ChildNotContainedChild.Ranges.insert({0x200, 0x300}); 486 ChildNotContained.Children.push_back(ChildNotContainedChild); 487 TestInlineInfoEncodeError(llvm::support::little, ChildNotContained, 488 ChildNotContainedErr); 489 TestInlineInfoEncodeError(llvm::support::big, ChildNotContained, 490 ChildNotContainedErr); 491 492 } 493 494 TEST(GSYMTest, TestInlineInfoDecodeErrors) { 495 // Test decoding InlineInfo objects that ensure we report an appropriate 496 // error message. 497 const llvm::support::endianness ByteOrder = llvm::support::little; 498 SmallString<512> Str; 499 raw_svector_ostream OutStrm(Str); 500 FileWriter FW(OutStrm, ByteOrder); 501 const uint64_t BaseAddr = 0x100; 502 TestInlineInfoDecodeError(ByteOrder, OutStrm.str(), BaseAddr, 503 "0x00000000: missing InlineInfo address ranges data"); 504 AddressRanges Ranges; 505 Ranges.insert({BaseAddr, BaseAddr+0x100}); 506 encodeRanges(Ranges, FW, BaseAddr); 507 TestInlineInfoDecodeError(ByteOrder, OutStrm.str(), BaseAddr, 508 "0x00000004: missing InlineInfo uint8_t indicating children"); 509 FW.writeU8(0); 510 TestInlineInfoDecodeError(ByteOrder, OutStrm.str(), BaseAddr, 511 "0x00000005: missing InlineInfo uint32_t for name"); 512 FW.writeU32(0); 513 TestInlineInfoDecodeError(ByteOrder, OutStrm.str(), BaseAddr, 514 "0x00000009: missing ULEB128 for InlineInfo call file"); 515 FW.writeU8(0); 516 TestInlineInfoDecodeError(ByteOrder, OutStrm.str(), BaseAddr, 517 "0x0000000a: missing ULEB128 for InlineInfo call line"); 518 } 519 520 TEST(GSYMTest, TestLineEntry) { 521 // test llvm::gsym::LineEntry structs. 522 const uint64_t ValidAddr = 0x1000; 523 const uint64_t InvalidFileIdx = 0; 524 const uint32_t ValidFileIdx = 1; 525 const uint32_t ValidLine = 5; 526 527 LineEntry Invalid; 528 EXPECT_FALSE(Invalid.isValid()); 529 // Make sure that an entry is invalid if it has a bad file index. 530 LineEntry BadFile(ValidAddr, InvalidFileIdx, ValidLine); 531 EXPECT_FALSE(BadFile.isValid()); 532 // Test operators 533 LineEntry E1(ValidAddr, ValidFileIdx, ValidLine); 534 LineEntry E2(ValidAddr, ValidFileIdx, ValidLine); 535 LineEntry DifferentAddr(ValidAddr + 1, ValidFileIdx, ValidLine); 536 LineEntry DifferentFile(ValidAddr, ValidFileIdx + 1, ValidLine); 537 LineEntry DifferentLine(ValidAddr, ValidFileIdx, ValidLine + 1); 538 EXPECT_TRUE(E1.isValid()); 539 EXPECT_EQ(E1, E2); 540 EXPECT_NE(E1, DifferentAddr); 541 EXPECT_NE(E1, DifferentFile); 542 EXPECT_NE(E1, DifferentLine); 543 EXPECT_LT(E1, DifferentAddr); 544 } 545 546 TEST(GSYMTest, TestStringTable) { 547 StringTable StrTab(StringRef("\0Hello\0World\0", 13)); 548 // Test extracting strings from a string table. 549 EXPECT_EQ(StrTab.getString(0), ""); 550 EXPECT_EQ(StrTab.getString(1), "Hello"); 551 EXPECT_EQ(StrTab.getString(7), "World"); 552 EXPECT_EQ(StrTab.getString(8), "orld"); 553 // Test pointing to last NULL terminator gets empty string. 554 EXPECT_EQ(StrTab.getString(12), ""); 555 // Test pointing to past end gets empty string. 556 EXPECT_EQ(StrTab.getString(13), ""); 557 } 558 559 static void TestFileWriterHelper(llvm::support::endianness ByteOrder) { 560 SmallString<512> Str; 561 raw_svector_ostream OutStrm(Str); 562 FileWriter FW(OutStrm, ByteOrder); 563 const int64_t MinSLEB = INT64_MIN; 564 const int64_t MaxSLEB = INT64_MAX; 565 const uint64_t MinULEB = 0; 566 const uint64_t MaxULEB = UINT64_MAX; 567 const uint8_t U8 = 0x10; 568 const uint16_t U16 = 0x1122; 569 const uint32_t U32 = 0x12345678; 570 const uint64_t U64 = 0x33445566778899aa; 571 const char *Hello = "hello"; 572 FW.writeU8(U8); 573 FW.writeU16(U16); 574 FW.writeU32(U32); 575 FW.writeU64(U64); 576 FW.alignTo(16); 577 const off_t FixupOffset = FW.tell(); 578 FW.writeU32(0); 579 FW.writeSLEB(MinSLEB); 580 FW.writeSLEB(MaxSLEB); 581 FW.writeULEB(MinULEB); 582 FW.writeULEB(MaxULEB); 583 FW.writeNullTerminated(Hello); 584 // Test Seek, Tell using Fixup32. 585 FW.fixup32(U32, FixupOffset); 586 587 std::string Bytes(OutStrm.str()); 588 uint8_t AddressSize = 4; 589 DataExtractor Data(Bytes, ByteOrder == llvm::support::little, AddressSize); 590 uint64_t Offset = 0; 591 EXPECT_EQ(Data.getU8(&Offset), U8); 592 EXPECT_EQ(Data.getU16(&Offset), U16); 593 EXPECT_EQ(Data.getU32(&Offset), U32); 594 EXPECT_EQ(Data.getU64(&Offset), U64); 595 Offset = alignTo(Offset, 16); 596 EXPECT_EQ(Data.getU32(&Offset), U32); 597 EXPECT_EQ(Data.getSLEB128(&Offset), MinSLEB); 598 EXPECT_EQ(Data.getSLEB128(&Offset), MaxSLEB); 599 EXPECT_EQ(Data.getULEB128(&Offset), MinULEB); 600 EXPECT_EQ(Data.getULEB128(&Offset), MaxULEB); 601 EXPECT_EQ(Data.getCStrRef(&Offset), StringRef(Hello)); 602 } 603 604 TEST(GSYMTest, TestFileWriter) { 605 TestFileWriterHelper(llvm::support::little); 606 TestFileWriterHelper(llvm::support::big); 607 } 608 609 TEST(GSYMTest, TestAddressRangeEncodeDecode) { 610 // Test encoding and decoding AddressRange objects. AddressRange objects 611 // are always stored as offsets from the a base address. The base address 612 // is the FunctionInfo's base address for function level ranges, and is 613 // the base address of the parent range for subranges. 614 SmallString<512> Str; 615 raw_svector_ostream OutStrm(Str); 616 const auto ByteOrder = llvm::support::endian::system_endianness(); 617 FileWriter FW(OutStrm, ByteOrder); 618 const uint64_t BaseAddr = 0x1000; 619 const AddressRange Range1(0x1000, 0x1010); 620 const AddressRange Range2(0x1020, 0x1030); 621 encodeRange(Range1, FW, BaseAddr); 622 encodeRange(Range2, FW, BaseAddr); 623 std::string Bytes(OutStrm.str()); 624 uint8_t AddressSize = 4; 625 DataExtractor Data(Bytes, ByteOrder == llvm::support::little, AddressSize); 626 627 AddressRange DecodedRange1, DecodedRange2; 628 uint64_t Offset = 0; 629 DecodedRange1 = decodeRange(Data, BaseAddr, Offset); 630 DecodedRange2 = decodeRange(Data, BaseAddr, Offset); 631 EXPECT_EQ(Range1, DecodedRange1); 632 EXPECT_EQ(Range2, DecodedRange2); 633 } 634 635 static void TestAddressRangeEncodeDecodeHelper(const AddressRanges &Ranges, 636 const uint64_t BaseAddr) { 637 SmallString<512> Str; 638 raw_svector_ostream OutStrm(Str); 639 const auto ByteOrder = llvm::support::endian::system_endianness(); 640 FileWriter FW(OutStrm, ByteOrder); 641 encodeRanges(Ranges, FW, BaseAddr); 642 643 std::string Bytes(OutStrm.str()); 644 uint8_t AddressSize = 4; 645 DataExtractor Data(Bytes, ByteOrder == llvm::support::little, AddressSize); 646 647 AddressRanges DecodedRanges; 648 uint64_t Offset = 0; 649 decodeRanges(DecodedRanges, Data, BaseAddr, Offset); 650 EXPECT_EQ(Ranges, DecodedRanges); 651 } 652 653 TEST(GSYMTest, TestAddressRangesEncodeDecode) { 654 // Test encoding and decoding AddressRanges. AddressRanges objects contain 655 // ranges that are stored as offsets from the a base address. The base address 656 // is the FunctionInfo's base address for function level ranges, and is the 657 // base address of the parent range for subranges. 658 const uint64_t BaseAddr = 0x1000; 659 660 // Test encoding and decoding with no ranges. 661 AddressRanges Ranges; 662 TestAddressRangeEncodeDecodeHelper(Ranges, BaseAddr); 663 664 // Test encoding and decoding with 1 range. 665 Ranges.insert(AddressRange(0x1000, 0x1010)); 666 TestAddressRangeEncodeDecodeHelper(Ranges, BaseAddr); 667 668 // Test encoding and decoding with multiple ranges. 669 Ranges.insert(AddressRange(0x1020, 0x1030)); 670 Ranges.insert(AddressRange(0x1050, 0x1070)); 671 TestAddressRangeEncodeDecodeHelper(Ranges, BaseAddr); 672 } 673 674 static void TestLineTableHelper(llvm::support::endianness ByteOrder, 675 const LineTable <) { 676 SmallString<512> Str; 677 raw_svector_ostream OutStrm(Str); 678 FileWriter FW(OutStrm, ByteOrder); 679 const uint64_t BaseAddr = LT[0].Addr; 680 llvm::Error Err = LT.encode(FW, BaseAddr); 681 ASSERT_FALSE(Err); 682 std::string Bytes(OutStrm.str()); 683 uint8_t AddressSize = 4; 684 DataExtractor Data(Bytes, ByteOrder == llvm::support::little, AddressSize); 685 llvm::Expected<LineTable> Decoded = LineTable::decode(Data, BaseAddr); 686 // Make sure decoding succeeded. 687 ASSERT_TRUE((bool)Decoded); 688 // Make sure decoded object is the same as the one we encoded. 689 EXPECT_EQ(LT, Decoded.get()); 690 } 691 692 TEST(GSYMTest, TestLineTable) { 693 const uint64_t StartAddr = 0x1000; 694 const uint32_t FileIdx = 1; 695 LineTable LT; 696 LineEntry Line0(StartAddr+0x000, FileIdx, 10); 697 LineEntry Line1(StartAddr+0x010, FileIdx, 11); 698 LineEntry Line2(StartAddr+0x100, FileIdx, 1000); 699 ASSERT_TRUE(LT.empty()); 700 ASSERT_EQ(LT.size(), (size_t)0); 701 LT.push(Line0); 702 ASSERT_EQ(LT.size(), (size_t)1); 703 LT.push(Line1); 704 LT.push(Line2); 705 LT.push(LineEntry(StartAddr+0x120, FileIdx, 900)); 706 LT.push(LineEntry(StartAddr+0x120, FileIdx, 2000)); 707 LT.push(LineEntry(StartAddr+0x121, FileIdx, 2001)); 708 LT.push(LineEntry(StartAddr+0x122, FileIdx, 2002)); 709 LT.push(LineEntry(StartAddr+0x123, FileIdx, 2003)); 710 ASSERT_FALSE(LT.empty()); 711 ASSERT_EQ(LT.size(), (size_t)8); 712 // Test operator[]. 713 ASSERT_EQ(LT[0], Line0); 714 ASSERT_EQ(LT[1], Line1); 715 ASSERT_EQ(LT[2], Line2); 716 717 // Test encoding and decoding line tables. 718 TestLineTableHelper(llvm::support::little, LT); 719 TestLineTableHelper(llvm::support::big, LT); 720 721 // Verify the clear method works as expected. 722 LT.clear(); 723 ASSERT_TRUE(LT.empty()); 724 ASSERT_EQ(LT.size(), (size_t)0); 725 726 LineTable LT1; 727 LineTable LT2; 728 729 // Test that two empty line tables are equal and neither are less than 730 // each other. 731 ASSERT_EQ(LT1, LT2); 732 ASSERT_FALSE(LT1 < LT1); 733 ASSERT_FALSE(LT1 < LT2); 734 ASSERT_FALSE(LT2 < LT1); 735 ASSERT_FALSE(LT2 < LT2); 736 737 // Test that a line table with less number of line entries is less than a 738 // line table with more line entries and that they are not equal. 739 LT2.push(Line0); 740 ASSERT_LT(LT1, LT2); 741 ASSERT_NE(LT1, LT2); 742 743 // Test that two line tables with the same entries are equal. 744 LT1.push(Line0); 745 ASSERT_EQ(LT1, LT2); 746 ASSERT_FALSE(LT1 < LT2); 747 ASSERT_FALSE(LT2 < LT2); 748 } 749 750 static void TestLineTableDecodeError(llvm::support::endianness ByteOrder, 751 StringRef Bytes, const uint64_t BaseAddr, 752 std::string ExpectedErrorMsg) { 753 uint8_t AddressSize = 4; 754 DataExtractor Data(Bytes, ByteOrder == llvm::support::little, AddressSize); 755 llvm::Expected<LineTable> Decoded = LineTable::decode(Data, BaseAddr); 756 // Make sure decoding fails. 757 ASSERT_FALSE((bool)Decoded); 758 // Make sure decoded object is the same as the one we encoded. 759 checkError(ExpectedErrorMsg, Decoded.takeError()); 760 } 761 762 TEST(GSYMTest, TestLineTableDecodeErrors) { 763 // Test decoding InlineInfo objects that ensure we report an appropriate 764 // error message. 765 const llvm::support::endianness ByteOrder = llvm::support::little; 766 SmallString<512> Str; 767 raw_svector_ostream OutStrm(Str); 768 FileWriter FW(OutStrm, ByteOrder); 769 const uint64_t BaseAddr = 0x100; 770 TestLineTableDecodeError(ByteOrder, OutStrm.str(), BaseAddr, 771 "0x00000000: missing LineTable MinDelta"); 772 FW.writeU8(1); // MinDelta (ULEB) 773 TestLineTableDecodeError(ByteOrder, OutStrm.str(), BaseAddr, 774 "0x00000001: missing LineTable MaxDelta"); 775 FW.writeU8(10); // MaxDelta (ULEB) 776 TestLineTableDecodeError(ByteOrder, OutStrm.str(), BaseAddr, 777 "0x00000002: missing LineTable FirstLine"); 778 FW.writeU8(20); // FirstLine (ULEB) 779 TestLineTableDecodeError(ByteOrder, OutStrm.str(), BaseAddr, 780 "0x00000003: EOF found before EndSequence"); 781 // Test a SetFile with the argument missing from the stream 782 FW.writeU8(1); // SetFile opcode (uint8_t) 783 TestLineTableDecodeError(ByteOrder, OutStrm.str(), BaseAddr, 784 "0x00000004: EOF found before SetFile value"); 785 FW.writeU8(5); // SetFile value as index (ULEB) 786 // Test a AdvancePC with the argument missing from the stream 787 FW.writeU8(2); // AdvancePC opcode (uint8_t) 788 TestLineTableDecodeError(ByteOrder, OutStrm.str(), BaseAddr, 789 "0x00000006: EOF found before AdvancePC value"); 790 FW.writeU8(20); // AdvancePC value as offset (ULEB) 791 // Test a AdvancePC with the argument missing from the stream 792 FW.writeU8(3); // AdvanceLine opcode (uint8_t) 793 TestLineTableDecodeError(ByteOrder, OutStrm.str(), BaseAddr, 794 "0x00000008: EOF found before AdvanceLine value"); 795 FW.writeU8(20); // AdvanceLine value as offset (LLEB) 796 } 797 798 TEST(GSYMTest, TestLineTableEncodeErrors) { 799 const uint64_t BaseAddr = 0x1000; 800 const uint32_t FileIdx = 1; 801 const llvm::support::endianness ByteOrder = llvm::support::little; 802 SmallString<512> Str; 803 raw_svector_ostream OutStrm(Str); 804 FileWriter FW(OutStrm, ByteOrder); 805 LineTable LT; 806 checkError("attempted to encode invalid LineTable object", 807 LT.encode(FW, BaseAddr)); 808 809 // Try to encode a line table where a line entry has an address that is less 810 // than BaseAddr and verify we get an appropriate error. 811 LineEntry Line0(BaseAddr+0x000, FileIdx, 10); 812 LineEntry Line1(BaseAddr+0x010, FileIdx, 11); 813 LT.push(Line0); 814 LT.push(Line1); 815 checkError("LineEntry has address 0x1000 which is less than the function " 816 "start address 0x1010", LT.encode(FW, BaseAddr+0x10)); 817 LT.clear(); 818 819 // Try to encode a line table where a line entries has an address that is less 820 // than BaseAddr and verify we get an appropriate error. 821 LT.push(Line1); 822 LT.push(Line0); 823 checkError("LineEntry in LineTable not in ascending order", 824 LT.encode(FW, BaseAddr)); 825 LT.clear(); 826 } 827 828 static void TestHeaderEncodeError(const Header &H, 829 std::string ExpectedErrorMsg) { 830 const support::endianness ByteOrder = llvm::support::little; 831 SmallString<512> Str; 832 raw_svector_ostream OutStrm(Str); 833 FileWriter FW(OutStrm, ByteOrder); 834 llvm::Error Err = H.encode(FW); 835 checkError(ExpectedErrorMsg, std::move(Err)); 836 } 837 838 static void TestHeaderDecodeError(StringRef Bytes, 839 std::string ExpectedErrorMsg) { 840 const support::endianness ByteOrder = llvm::support::little; 841 uint8_t AddressSize = 4; 842 DataExtractor Data(Bytes, ByteOrder == llvm::support::little, AddressSize); 843 llvm::Expected<Header> Decoded = Header::decode(Data); 844 // Make sure decoding fails. 845 ASSERT_FALSE((bool)Decoded); 846 // Make sure decoded object is the same as the one we encoded. 847 checkError(ExpectedErrorMsg, Decoded.takeError()); 848 } 849 850 // Populate a GSYM header with valid values. 851 static void InitHeader(Header &H) { 852 H.Magic = GSYM_MAGIC; 853 H.Version = GSYM_VERSION; 854 H.AddrOffSize = 4; 855 H.UUIDSize = 16; 856 H.BaseAddress = 0x1000; 857 H.NumAddresses = 1; 858 H.StrtabOffset= 0x2000; 859 H.StrtabSize = 0x1000; 860 for (size_t i=0; i<GSYM_MAX_UUID_SIZE; ++i) { 861 if (i < H.UUIDSize) 862 H.UUID[i] = i; 863 else 864 H.UUID[i] = 0; 865 } 866 } 867 868 TEST(GSYMTest, TestHeaderEncodeErrors) { 869 Header H; 870 InitHeader(H); 871 H.Magic = 12; 872 TestHeaderEncodeError(H, "invalid GSYM magic 0x0000000c"); 873 InitHeader(H); 874 H.Version = 12; 875 TestHeaderEncodeError(H, "unsupported GSYM version 12"); 876 InitHeader(H); 877 H.AddrOffSize = 12; 878 TestHeaderEncodeError(H, "invalid address offset size 12"); 879 InitHeader(H); 880 H.UUIDSize = 128; 881 TestHeaderEncodeError(H, "invalid UUID size 128"); 882 } 883 884 TEST(GSYMTest, TestHeaderDecodeErrors) { 885 const llvm::support::endianness ByteOrder = llvm::support::little; 886 SmallString<512> Str; 887 raw_svector_ostream OutStrm(Str); 888 FileWriter FW(OutStrm, ByteOrder); 889 Header H; 890 InitHeader(H); 891 llvm::Error Err = H.encode(FW); 892 ASSERT_FALSE(Err); 893 FW.fixup32(12, offsetof(Header, Magic)); 894 TestHeaderDecodeError(OutStrm.str(), "invalid GSYM magic 0x0000000c"); 895 FW.fixup32(GSYM_MAGIC, offsetof(Header, Magic)); 896 FW.fixup32(12, offsetof(Header, Version)); 897 TestHeaderDecodeError(OutStrm.str(), "unsupported GSYM version 12"); 898 FW.fixup32(GSYM_VERSION, offsetof(Header, Version)); 899 FW.fixup32(12, offsetof(Header, AddrOffSize)); 900 TestHeaderDecodeError(OutStrm.str(), "invalid address offset size 12"); 901 FW.fixup32(4, offsetof(Header, AddrOffSize)); 902 FW.fixup32(128, offsetof(Header, UUIDSize)); 903 TestHeaderDecodeError(OutStrm.str(), "invalid UUID size 128"); 904 } 905 906 static void TestHeaderEncodeDecode(const Header &H, 907 support::endianness ByteOrder) { 908 uint8_t AddressSize = 4; 909 SmallString<512> Str; 910 raw_svector_ostream OutStrm(Str); 911 FileWriter FW(OutStrm, ByteOrder); 912 llvm::Error Err = H.encode(FW); 913 ASSERT_FALSE(Err); 914 std::string Bytes(OutStrm.str()); 915 DataExtractor Data(Bytes, ByteOrder == llvm::support::little, AddressSize); 916 llvm::Expected<Header> Decoded = Header::decode(Data); 917 // Make sure decoding succeeded. 918 ASSERT_TRUE((bool)Decoded); 919 EXPECT_EQ(H, Decoded.get()); 920 921 } 922 TEST(GSYMTest, TestHeaderEncodeDecode) { 923 Header H; 924 InitHeader(H); 925 TestHeaderEncodeDecode(H, llvm::support::little); 926 TestHeaderEncodeDecode(H, llvm::support::big); 927 } 928 929 static void TestGsymCreatorEncodeError(llvm::support::endianness ByteOrder, 930 const GsymCreator &GC, 931 std::string ExpectedErrorMsg) { 932 SmallString<512> Str; 933 raw_svector_ostream OutStrm(Str); 934 FileWriter FW(OutStrm, ByteOrder); 935 llvm::Error Err = GC.encode(FW); 936 ASSERT_TRUE(bool(Err)); 937 checkError(ExpectedErrorMsg, std::move(Err)); 938 } 939 940 TEST(GSYMTest, TestGsymCreatorEncodeErrors) { 941 const uint8_t ValidUUID[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 942 14, 15, 16}; 943 const uint8_t InvalidUUID[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 944 14, 15, 16, 17, 18, 19, 20, 21}; 945 // Verify we get an error when trying to encode an GsymCreator with no 946 // function infos. We shouldn't be saving a GSYM file in this case since 947 // there is nothing inside of it. 948 GsymCreator GC; 949 TestGsymCreatorEncodeError(llvm::support::little, GC, 950 "no functions to encode"); 951 const uint64_t FuncAddr = 0x1000; 952 const uint64_t FuncSize = 0x100; 953 const uint32_t FuncName = GC.insertString("foo"); 954 // Verify we get an error trying to encode a GsymCreator that isn't 955 // finalized. 956 GC.addFunctionInfo(FunctionInfo(FuncAddr, FuncSize, FuncName)); 957 TestGsymCreatorEncodeError(llvm::support::little, GC, 958 "GsymCreator wasn't finalized prior to encoding"); 959 std::string finalizeIssues; 960 raw_string_ostream OS(finalizeIssues); 961 llvm::Error finalizeErr = GC.finalize(OS); 962 ASSERT_FALSE(bool(finalizeErr)); 963 finalizeErr = GC.finalize(OS); 964 ASSERT_TRUE(bool(finalizeErr)); 965 checkError("already finalized", std::move(finalizeErr)); 966 // Verify we get an error trying to encode a GsymCreator with a UUID that is 967 // too long. 968 GC.setUUID(InvalidUUID); 969 TestGsymCreatorEncodeError(llvm::support::little, GC, 970 "invalid UUID size 21"); 971 GC.setUUID(ValidUUID); 972 // Verify errors are propagated when we try to encoding an invalid line 973 // table. 974 GC.forEachFunctionInfo([](FunctionInfo &FI) -> bool { 975 FI.OptLineTable = LineTable(); // Invalid line table. 976 return false; // Stop iterating 977 }); 978 TestGsymCreatorEncodeError(llvm::support::little, GC, 979 "attempted to encode invalid LineTable object"); 980 // Verify errors are propagated when we try to encoding an invalid inline 981 // info. 982 GC.forEachFunctionInfo([](FunctionInfo &FI) -> bool { 983 FI.OptLineTable = llvm::None; 984 FI.Inline = InlineInfo(); // Invalid InlineInfo. 985 return false; // Stop iterating 986 }); 987 TestGsymCreatorEncodeError(llvm::support::little, GC, 988 "attempted to encode invalid InlineInfo object"); 989 } 990 991 static void Compare(const GsymCreator &GC, const GsymReader &GR) { 992 // Verify that all of the data in a GsymCreator is correctly decoded from 993 // a GsymReader. To do this, we iterator over 994 GC.forEachFunctionInfo([&](const FunctionInfo &FI) -> bool { 995 auto DecodedFI = GR.getFunctionInfo(FI.Range.start()); 996 EXPECT_TRUE(bool(DecodedFI)); 997 EXPECT_EQ(FI, *DecodedFI); 998 return true; // Keep iterating over all FunctionInfo objects. 999 }); 1000 } 1001 1002 static void TestEncodeDecode(const GsymCreator &GC, 1003 support::endianness ByteOrder, uint16_t Version, 1004 uint8_t AddrOffSize, uint64_t BaseAddress, 1005 uint32_t NumAddresses, ArrayRef<uint8_t> UUID) { 1006 SmallString<512> Str; 1007 raw_svector_ostream OutStrm(Str); 1008 FileWriter FW(OutStrm, ByteOrder); 1009 llvm::Error Err = GC.encode(FW); 1010 ASSERT_FALSE((bool)Err); 1011 Expected<GsymReader> GR = GsymReader::copyBuffer(OutStrm.str()); 1012 ASSERT_TRUE(bool(GR)); 1013 const Header &Hdr = GR->getHeader(); 1014 EXPECT_EQ(Hdr.Version, Version); 1015 EXPECT_EQ(Hdr.AddrOffSize, AddrOffSize); 1016 EXPECT_EQ(Hdr.UUIDSize, UUID.size()); 1017 EXPECT_EQ(Hdr.BaseAddress, BaseAddress); 1018 EXPECT_EQ(Hdr.NumAddresses, NumAddresses); 1019 EXPECT_EQ(ArrayRef<uint8_t>(Hdr.UUID, Hdr.UUIDSize), UUID); 1020 Compare(GC, GR.get()); 1021 } 1022 1023 TEST(GSYMTest, TestGsymCreator1ByteAddrOffsets) { 1024 uint8_t UUID[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}; 1025 GsymCreator GC; 1026 GC.setUUID(UUID); 1027 constexpr uint64_t BaseAddr = 0x1000; 1028 constexpr uint8_t AddrOffSize = 1; 1029 const uint32_t Func1Name = GC.insertString("foo"); 1030 const uint32_t Func2Name = GC.insertString("bar"); 1031 GC.addFunctionInfo(FunctionInfo(BaseAddr+0x00, 0x10, Func1Name)); 1032 GC.addFunctionInfo(FunctionInfo(BaseAddr+0x20, 0x10, Func2Name)); 1033 Error Err = GC.finalize(llvm::nulls()); 1034 ASSERT_FALSE(Err); 1035 TestEncodeDecode(GC, llvm::support::little, 1036 GSYM_VERSION, 1037 AddrOffSize, 1038 BaseAddr, 1039 2, // NumAddresses 1040 ArrayRef<uint8_t>(UUID)); 1041 TestEncodeDecode(GC, llvm::support::big, 1042 GSYM_VERSION, 1043 AddrOffSize, 1044 BaseAddr, 1045 2, // NumAddresses 1046 ArrayRef<uint8_t>(UUID)); 1047 } 1048 1049 TEST(GSYMTest, TestGsymCreator2ByteAddrOffsets) { 1050 uint8_t UUID[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}; 1051 GsymCreator GC; 1052 GC.setUUID(UUID); 1053 constexpr uint64_t BaseAddr = 0x1000; 1054 constexpr uint8_t AddrOffSize = 2; 1055 const uint32_t Func1Name = GC.insertString("foo"); 1056 const uint32_t Func2Name = GC.insertString("bar"); 1057 GC.addFunctionInfo(FunctionInfo(BaseAddr+0x000, 0x100, Func1Name)); 1058 GC.addFunctionInfo(FunctionInfo(BaseAddr+0x200, 0x100, Func2Name)); 1059 Error Err = GC.finalize(llvm::nulls()); 1060 ASSERT_FALSE(Err); 1061 TestEncodeDecode(GC, llvm::support::little, 1062 GSYM_VERSION, 1063 AddrOffSize, 1064 BaseAddr, 1065 2, // NumAddresses 1066 ArrayRef<uint8_t>(UUID)); 1067 TestEncodeDecode(GC, llvm::support::big, 1068 GSYM_VERSION, 1069 AddrOffSize, 1070 BaseAddr, 1071 2, // NumAddresses 1072 ArrayRef<uint8_t>(UUID)); 1073 } 1074 1075 TEST(GSYMTest, TestGsymCreator4ByteAddrOffsets) { 1076 uint8_t UUID[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}; 1077 GsymCreator GC; 1078 GC.setUUID(UUID); 1079 constexpr uint64_t BaseAddr = 0x1000; 1080 constexpr uint8_t AddrOffSize = 4; 1081 const uint32_t Func1Name = GC.insertString("foo"); 1082 const uint32_t Func2Name = GC.insertString("bar"); 1083 GC.addFunctionInfo(FunctionInfo(BaseAddr+0x000, 0x100, Func1Name)); 1084 GC.addFunctionInfo(FunctionInfo(BaseAddr+0x20000, 0x100, Func2Name)); 1085 Error Err = GC.finalize(llvm::nulls()); 1086 ASSERT_FALSE(Err); 1087 TestEncodeDecode(GC, llvm::support::little, 1088 GSYM_VERSION, 1089 AddrOffSize, 1090 BaseAddr, 1091 2, // NumAddresses 1092 ArrayRef<uint8_t>(UUID)); 1093 TestEncodeDecode(GC, llvm::support::big, 1094 GSYM_VERSION, 1095 AddrOffSize, 1096 BaseAddr, 1097 2, // NumAddresses 1098 ArrayRef<uint8_t>(UUID)); 1099 } 1100 1101 TEST(GSYMTest, TestGsymCreator8ByteAddrOffsets) { 1102 uint8_t UUID[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}; 1103 GsymCreator GC; 1104 GC.setUUID(UUID); 1105 constexpr uint64_t BaseAddr = 0x1000; 1106 constexpr uint8_t AddrOffSize = 8; 1107 const uint32_t Func1Name = GC.insertString("foo"); 1108 const uint32_t Func2Name = GC.insertString("bar"); 1109 GC.addFunctionInfo(FunctionInfo(BaseAddr+0x000, 0x100, Func1Name)); 1110 GC.addFunctionInfo(FunctionInfo(BaseAddr+0x100000000, 0x100, Func2Name)); 1111 Error Err = GC.finalize(llvm::nulls()); 1112 ASSERT_FALSE(Err); 1113 TestEncodeDecode(GC, llvm::support::little, 1114 GSYM_VERSION, 1115 AddrOffSize, 1116 BaseAddr, 1117 2, // NumAddresses 1118 ArrayRef<uint8_t>(UUID)); 1119 TestEncodeDecode(GC, llvm::support::big, 1120 GSYM_VERSION, 1121 AddrOffSize, 1122 BaseAddr, 1123 2, // NumAddresses 1124 ArrayRef<uint8_t>(UUID)); 1125 } 1126 1127 static void VerifyFunctionInfo(const GsymReader &GR, uint64_t Addr, 1128 const FunctionInfo &FI) { 1129 auto ExpFI = GR.getFunctionInfo(Addr); 1130 ASSERT_TRUE(bool(ExpFI)); 1131 ASSERT_EQ(FI, ExpFI.get()); 1132 } 1133 1134 static void VerifyFunctionInfoError(const GsymReader &GR, uint64_t Addr, 1135 std::string ErrMessage) { 1136 auto ExpFI = GR.getFunctionInfo(Addr); 1137 ASSERT_FALSE(bool(ExpFI)); 1138 checkError(ErrMessage, ExpFI.takeError()); 1139 } 1140 1141 TEST(GSYMTest, TestGsymReader) { 1142 uint8_t UUID[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}; 1143 GsymCreator GC; 1144 GC.setUUID(UUID); 1145 constexpr uint64_t BaseAddr = 0x1000; 1146 constexpr uint64_t Func1Addr = BaseAddr; 1147 constexpr uint64_t Func2Addr = BaseAddr+0x20; 1148 constexpr uint64_t FuncSize = 0x10; 1149 const uint32_t Func1Name = GC.insertString("foo"); 1150 const uint32_t Func2Name = GC.insertString("bar"); 1151 const auto ByteOrder = support::endian::system_endianness(); 1152 GC.addFunctionInfo(FunctionInfo(Func1Addr, FuncSize, Func1Name)); 1153 GC.addFunctionInfo(FunctionInfo(Func2Addr, FuncSize, Func2Name)); 1154 Error FinalizeErr = GC.finalize(llvm::nulls()); 1155 ASSERT_FALSE(FinalizeErr); 1156 SmallString<512> Str; 1157 raw_svector_ostream OutStrm(Str); 1158 FileWriter FW(OutStrm, ByteOrder); 1159 llvm::Error Err = GC.encode(FW); 1160 ASSERT_FALSE((bool)Err); 1161 if (auto ExpectedGR = GsymReader::copyBuffer(OutStrm.str())) { 1162 const GsymReader &GR = ExpectedGR.get(); 1163 VerifyFunctionInfoError(GR, Func1Addr-1, "address 0xfff is not in GSYM"); 1164 1165 FunctionInfo Func1(Func1Addr, FuncSize, Func1Name); 1166 VerifyFunctionInfo(GR, Func1Addr, Func1); 1167 VerifyFunctionInfo(GR, Func1Addr+1, Func1); 1168 VerifyFunctionInfo(GR, Func1Addr+FuncSize-1, Func1); 1169 VerifyFunctionInfoError(GR, Func1Addr+FuncSize, 1170 "address 0x1010 is not in GSYM"); 1171 VerifyFunctionInfoError(GR, Func2Addr-1, "address 0x101f is not in GSYM"); 1172 FunctionInfo Func2(Func2Addr, FuncSize, Func2Name); 1173 VerifyFunctionInfo(GR, Func2Addr, Func2); 1174 VerifyFunctionInfo(GR, Func2Addr+1, Func2); 1175 VerifyFunctionInfo(GR, Func2Addr+FuncSize-1, Func2); 1176 VerifyFunctionInfoError(GR, Func2Addr+FuncSize, 1177 "address 0x1030 is not in GSYM"); 1178 } 1179 } 1180 1181 TEST(GSYMTest, TestGsymLookups) { 1182 // Test creating a GSYM file with a function that has a inline information. 1183 // Verify that lookups work correctly. Lookups do not decode the entire 1184 // FunctionInfo or InlineInfo, they only extract information needed for the 1185 // lookup to happen which avoids allocations which can slow down 1186 // symbolication. 1187 GsymCreator GC; 1188 FunctionInfo FI(0x1000, 0x100, GC.insertString("main")); 1189 const auto ByteOrder = support::endian::system_endianness(); 1190 FI.OptLineTable = LineTable(); 1191 const uint32_t MainFileIndex = GC.insertFile("/tmp/main.c"); 1192 const uint32_t FooFileIndex = GC.insertFile("/tmp/foo.h"); 1193 FI.OptLineTable->push(LineEntry(0x1000, MainFileIndex, 5)); 1194 FI.OptLineTable->push(LineEntry(0x1010, FooFileIndex, 10)); 1195 FI.OptLineTable->push(LineEntry(0x1012, FooFileIndex, 20)); 1196 FI.OptLineTable->push(LineEntry(0x1014, FooFileIndex, 11)); 1197 FI.OptLineTable->push(LineEntry(0x1016, FooFileIndex, 30)); 1198 FI.OptLineTable->push(LineEntry(0x1018, FooFileIndex, 12)); 1199 FI.OptLineTable->push(LineEntry(0x1020, MainFileIndex, 8)); 1200 FI.Inline = InlineInfo(); 1201 1202 FI.Inline->Name = GC.insertString("inline1"); 1203 FI.Inline->CallFile = MainFileIndex; 1204 FI.Inline->CallLine = 6; 1205 FI.Inline->Ranges.insert(AddressRange(0x1010, 0x1020)); 1206 InlineInfo Inline2; 1207 Inline2.Name = GC.insertString("inline2"); 1208 Inline2.CallFile = FooFileIndex; 1209 Inline2.CallLine = 33; 1210 Inline2.Ranges.insert(AddressRange(0x1012, 0x1014)); 1211 FI.Inline->Children.emplace_back(Inline2); 1212 InlineInfo Inline3; 1213 Inline3.Name = GC.insertString("inline3"); 1214 Inline3.CallFile = FooFileIndex; 1215 Inline3.CallLine = 35; 1216 Inline3.Ranges.insert(AddressRange(0x1016, 0x1018)); 1217 FI.Inline->Children.emplace_back(Inline3); 1218 GC.addFunctionInfo(std::move(FI)); 1219 Error FinalizeErr = GC.finalize(llvm::nulls()); 1220 ASSERT_FALSE(FinalizeErr); 1221 SmallString<512> Str; 1222 raw_svector_ostream OutStrm(Str); 1223 FileWriter FW(OutStrm, ByteOrder); 1224 llvm::Error Err = GC.encode(FW); 1225 ASSERT_FALSE((bool)Err); 1226 Expected<GsymReader> GR = GsymReader::copyBuffer(OutStrm.str()); 1227 ASSERT_TRUE(bool(GR)); 1228 1229 // Verify inline info is correct when doing lookups. 1230 auto LR = GR->lookup(0x1000); 1231 ASSERT_THAT_EXPECTED(LR, Succeeded()); 1232 EXPECT_THAT(LR->Locations, 1233 testing::ElementsAre(SourceLocation{"main", "/tmp", "main.c", 5})); 1234 LR = GR->lookup(0x100F); 1235 ASSERT_THAT_EXPECTED(LR, Succeeded()); 1236 EXPECT_THAT(LR->Locations, 1237 testing::ElementsAre(SourceLocation{"main", "/tmp", "main.c", 5, 15})); 1238 1239 LR = GR->lookup(0x1010); 1240 ASSERT_THAT_EXPECTED(LR, Succeeded()); 1241 1242 EXPECT_THAT(LR->Locations, 1243 testing::ElementsAre(SourceLocation{"inline1", "/tmp", "foo.h", 10}, 1244 SourceLocation{"main", "/tmp", "main.c", 6, 16})); 1245 1246 LR = GR->lookup(0x1012); 1247 ASSERT_THAT_EXPECTED(LR, Succeeded()); 1248 EXPECT_THAT(LR->Locations, 1249 testing::ElementsAre(SourceLocation{"inline2", "/tmp", "foo.h", 20}, 1250 SourceLocation{"inline1", "/tmp", "foo.h", 33, 2}, 1251 SourceLocation{"main", "/tmp", "main.c", 6, 18})); 1252 1253 LR = GR->lookup(0x1014); 1254 ASSERT_THAT_EXPECTED(LR, Succeeded()); 1255 EXPECT_THAT(LR->Locations, 1256 testing::ElementsAre(SourceLocation{"inline1", "/tmp", "foo.h", 11, 4}, 1257 SourceLocation{"main", "/tmp", "main.c", 6, 20})); 1258 1259 LR = GR->lookup(0x1016); 1260 ASSERT_THAT_EXPECTED(LR, Succeeded()); 1261 EXPECT_THAT(LR->Locations, 1262 testing::ElementsAre(SourceLocation{"inline3", "/tmp", "foo.h", 30}, 1263 SourceLocation{"inline1", "/tmp", "foo.h", 35, 6}, 1264 SourceLocation{"main", "/tmp", "main.c", 6, 22})); 1265 1266 LR = GR->lookup(0x1018); 1267 ASSERT_THAT_EXPECTED(LR, Succeeded()); 1268 EXPECT_THAT(LR->Locations, 1269 testing::ElementsAre(SourceLocation{"inline1", "/tmp", "foo.h", 12, 8}, 1270 SourceLocation{"main", "/tmp", "main.c", 6, 24})); 1271 1272 LR = GR->lookup(0x1020); 1273 ASSERT_THAT_EXPECTED(LR, Succeeded()); 1274 EXPECT_THAT(LR->Locations, 1275 testing::ElementsAre(SourceLocation{"main", "/tmp", "main.c", 8, 32})); 1276 } 1277 1278 1279 TEST(GSYMTest, TestDWARFFunctionWithAddresses) { 1280 // Create a single compile unit with a single function and make sure it gets 1281 // converted to DWARF correctly. The function's address range is in where 1282 // DW_AT_low_pc and DW_AT_high_pc are both addresses. 1283 StringRef yamldata = R"( 1284 debug_str: 1285 - '' 1286 - /tmp/main.c 1287 - main 1288 debug_abbrev: 1289 - Table: 1290 - Code: 0x00000001 1291 Tag: DW_TAG_compile_unit 1292 Children: DW_CHILDREN_yes 1293 Attributes: 1294 - Attribute: DW_AT_name 1295 Form: DW_FORM_strp 1296 - Attribute: DW_AT_low_pc 1297 Form: DW_FORM_addr 1298 - Attribute: DW_AT_high_pc 1299 Form: DW_FORM_addr 1300 - Attribute: DW_AT_language 1301 Form: DW_FORM_data2 1302 - Code: 0x00000002 1303 Tag: DW_TAG_subprogram 1304 Children: DW_CHILDREN_no 1305 Attributes: 1306 - Attribute: DW_AT_name 1307 Form: DW_FORM_strp 1308 - Attribute: DW_AT_low_pc 1309 Form: DW_FORM_addr 1310 - Attribute: DW_AT_high_pc 1311 Form: DW_FORM_addr 1312 debug_info: 1313 - Version: 4 1314 AddrSize: 8 1315 Entries: 1316 - AbbrCode: 0x00000001 1317 Values: 1318 - Value: 0x0000000000000001 1319 - Value: 0x0000000000001000 1320 - Value: 0x0000000000002000 1321 - Value: 0x0000000000000004 1322 - AbbrCode: 0x00000002 1323 Values: 1324 - Value: 0x000000000000000D 1325 - Value: 0x0000000000001000 1326 - Value: 0x0000000000002000 1327 - AbbrCode: 0x00000000 1328 )"; 1329 auto ErrOrSections = DWARFYAML::emitDebugSections(yamldata); 1330 ASSERT_THAT_EXPECTED(ErrOrSections, Succeeded()); 1331 std::unique_ptr<DWARFContext> DwarfContext = 1332 DWARFContext::create(*ErrOrSections, 8); 1333 ASSERT_TRUE(DwarfContext.get() != nullptr); 1334 auto &OS = llvm::nulls(); 1335 GsymCreator GC; 1336 DwarfTransformer DT(*DwarfContext, OS, GC); 1337 const uint32_t ThreadCount = 1; 1338 ASSERT_THAT_ERROR(DT.convert(ThreadCount), Succeeded()); 1339 ASSERT_THAT_ERROR(GC.finalize(OS), Succeeded()); 1340 SmallString<512> Str; 1341 raw_svector_ostream OutStrm(Str); 1342 const auto ByteOrder = support::endian::system_endianness(); 1343 FileWriter FW(OutStrm, ByteOrder); 1344 ASSERT_THAT_ERROR(GC.encode(FW), Succeeded()); 1345 Expected<GsymReader> GR = GsymReader::copyBuffer(OutStrm.str()); 1346 ASSERT_THAT_EXPECTED(GR, Succeeded()); 1347 // There should only be one function in our GSYM. 1348 EXPECT_EQ(GR->getNumAddresses(), 1u); 1349 auto ExpFI = GR->getFunctionInfo(0x1000); 1350 ASSERT_THAT_EXPECTED(ExpFI, Succeeded()); 1351 ASSERT_EQ(ExpFI->Range, AddressRange(0x1000, 0x2000)); 1352 EXPECT_FALSE(ExpFI->OptLineTable.hasValue()); 1353 EXPECT_FALSE(ExpFI->Inline.hasValue()); 1354 } 1355 1356 TEST(GSYMTest, TestDWARFFunctionWithAddressAndOffset) { 1357 // Create a single compile unit with a single function and make sure it gets 1358 // converted to DWARF correctly. The function's address range is in where 1359 // DW_AT_low_pc is an address and the DW_AT_high_pc is an offset. 1360 StringRef yamldata = R"( 1361 debug_str: 1362 - '' 1363 - /tmp/main.c 1364 - main 1365 debug_abbrev: 1366 - Table: 1367 - Code: 0x00000001 1368 Tag: DW_TAG_compile_unit 1369 Children: DW_CHILDREN_yes 1370 Attributes: 1371 - Attribute: DW_AT_name 1372 Form: DW_FORM_strp 1373 - Attribute: DW_AT_low_pc 1374 Form: DW_FORM_addr 1375 - Attribute: DW_AT_high_pc 1376 Form: DW_FORM_data4 1377 - Attribute: DW_AT_language 1378 Form: DW_FORM_data2 1379 - Code: 0x00000002 1380 Tag: DW_TAG_subprogram 1381 Children: DW_CHILDREN_no 1382 Attributes: 1383 - Attribute: DW_AT_name 1384 Form: DW_FORM_strp 1385 - Attribute: DW_AT_low_pc 1386 Form: DW_FORM_addr 1387 - Attribute: DW_AT_high_pc 1388 Form: DW_FORM_data4 1389 debug_info: 1390 - Version: 4 1391 AddrSize: 8 1392 Entries: 1393 - AbbrCode: 0x00000001 1394 Values: 1395 - Value: 0x0000000000000001 1396 - Value: 0x0000000000001000 1397 - Value: 0x0000000000001000 1398 - Value: 0x0000000000000004 1399 - AbbrCode: 0x00000002 1400 Values: 1401 - Value: 0x000000000000000D 1402 - Value: 0x0000000000001000 1403 - Value: 0x0000000000001000 1404 - AbbrCode: 0x00000000 1405 )"; 1406 auto ErrOrSections = DWARFYAML::emitDebugSections(yamldata); 1407 ASSERT_THAT_EXPECTED(ErrOrSections, Succeeded()); 1408 std::unique_ptr<DWARFContext> DwarfContext = 1409 DWARFContext::create(*ErrOrSections, 8); 1410 ASSERT_TRUE(DwarfContext.get() != nullptr); 1411 auto &OS = llvm::nulls(); 1412 GsymCreator GC; 1413 DwarfTransformer DT(*DwarfContext, OS, GC); 1414 const uint32_t ThreadCount = 1; 1415 ASSERT_THAT_ERROR(DT.convert(ThreadCount), Succeeded()); 1416 ASSERT_THAT_ERROR(GC.finalize(OS), Succeeded()); 1417 SmallString<512> Str; 1418 raw_svector_ostream OutStrm(Str); 1419 const auto ByteOrder = support::endian::system_endianness(); 1420 FileWriter FW(OutStrm, ByteOrder); 1421 ASSERT_THAT_ERROR(GC.encode(FW), Succeeded()); 1422 Expected<GsymReader> GR = GsymReader::copyBuffer(OutStrm.str()); 1423 ASSERT_THAT_EXPECTED(GR, Succeeded()); 1424 // There should only be one function in our GSYM. 1425 EXPECT_EQ(GR->getNumAddresses(), 1u); 1426 auto ExpFI = GR->getFunctionInfo(0x1000); 1427 ASSERT_THAT_EXPECTED(ExpFI, Succeeded()); 1428 ASSERT_EQ(ExpFI->Range, AddressRange(0x1000, 0x2000)); 1429 EXPECT_FALSE(ExpFI->OptLineTable.hasValue()); 1430 EXPECT_FALSE(ExpFI->Inline.hasValue()); 1431 } 1432 1433 TEST(GSYMTest, TestDWARFStructMethodNoMangled) { 1434 // Sometimes the compiler will omit the mangled name in the DWARF for static 1435 // and member functions of classes and structs. This test verifies that the 1436 // fully qualified name of the method is computed and used as the string for 1437 // the function in the GSYM in these cases. Otherwise we might just get a 1438 // function name like "erase" instead of "std::vector<int>::erase". 1439 StringRef yamldata = R"( 1440 debug_str: 1441 - '' 1442 - /tmp/main.c 1443 - Foo 1444 - dump 1445 - this 1446 debug_abbrev: 1447 - Table: 1448 - Code: 0x00000001 1449 Tag: DW_TAG_compile_unit 1450 Children: DW_CHILDREN_yes 1451 Attributes: 1452 - Attribute: DW_AT_name 1453 Form: DW_FORM_strp 1454 - Attribute: DW_AT_low_pc 1455 Form: DW_FORM_addr 1456 - Attribute: DW_AT_high_pc 1457 Form: DW_FORM_addr 1458 - Attribute: DW_AT_language 1459 Form: DW_FORM_data2 1460 - Code: 0x00000002 1461 Tag: DW_TAG_structure_type 1462 Children: DW_CHILDREN_yes 1463 Attributes: 1464 - Attribute: DW_AT_name 1465 Form: DW_FORM_strp 1466 - Code: 0x00000003 1467 Tag: DW_TAG_subprogram 1468 Children: DW_CHILDREN_yes 1469 Attributes: 1470 - Attribute: DW_AT_name 1471 Form: DW_FORM_strp 1472 - Attribute: DW_AT_low_pc 1473 Form: DW_FORM_addr 1474 - Attribute: DW_AT_high_pc 1475 Form: DW_FORM_addr 1476 - Code: 0x00000004 1477 Tag: DW_TAG_formal_parameter 1478 Children: DW_CHILDREN_no 1479 Attributes: 1480 - Attribute: DW_AT_name 1481 Form: DW_FORM_strp 1482 - Attribute: DW_AT_type 1483 Form: DW_FORM_ref4 1484 - Attribute: DW_AT_artificial 1485 Form: DW_FORM_flag_present 1486 debug_info: 1487 - Version: 4 1488 AddrSize: 8 1489 Entries: 1490 - AbbrCode: 0x00000001 1491 Values: 1492 - Value: 0x0000000000000001 1493 - Value: 0x0000000000001000 1494 - Value: 0x0000000000002000 1495 - Value: 0x0000000000000004 1496 - AbbrCode: 0x00000002 1497 Values: 1498 - Value: 0x000000000000000D 1499 - AbbrCode: 0x00000003 1500 Values: 1501 - Value: 0x0000000000000011 1502 - Value: 0x0000000000001000 1503 - Value: 0x0000000000002000 1504 - AbbrCode: 0x00000004 1505 Values: 1506 - Value: 0x0000000000000016 1507 - Value: 0x0000000000000022 1508 - Value: 0x0000000000000001 1509 - AbbrCode: 0x00000000 1510 - AbbrCode: 0x00000000 1511 - AbbrCode: 0x00000000 1512 )"; 1513 auto ErrOrSections = DWARFYAML::emitDebugSections(yamldata); 1514 ASSERT_THAT_EXPECTED(ErrOrSections, Succeeded()); 1515 std::unique_ptr<DWARFContext> DwarfContext = 1516 DWARFContext::create(*ErrOrSections, 8); 1517 ASSERT_TRUE(DwarfContext.get() != nullptr); 1518 auto &OS = llvm::nulls(); 1519 GsymCreator GC; 1520 DwarfTransformer DT(*DwarfContext, OS, GC); 1521 const uint32_t ThreadCount = 1; 1522 ASSERT_THAT_ERROR(DT.convert(ThreadCount), Succeeded()); 1523 ASSERT_THAT_ERROR(GC.finalize(OS), Succeeded()); 1524 SmallString<512> Str; 1525 raw_svector_ostream OutStrm(Str); 1526 const auto ByteOrder = support::endian::system_endianness(); 1527 FileWriter FW(OutStrm, ByteOrder); 1528 ASSERT_THAT_ERROR(GC.encode(FW), Succeeded()); 1529 Expected<GsymReader> GR = GsymReader::copyBuffer(OutStrm.str()); 1530 ASSERT_THAT_EXPECTED(GR, Succeeded()); 1531 // There should only be one function in our GSYM. 1532 EXPECT_EQ(GR->getNumAddresses(), 1u); 1533 auto ExpFI = GR->getFunctionInfo(0x1000); 1534 ASSERT_THAT_EXPECTED(ExpFI, Succeeded()); 1535 ASSERT_EQ(ExpFI->Range, AddressRange(0x1000, 0x2000)); 1536 EXPECT_FALSE(ExpFI->OptLineTable.hasValue()); 1537 EXPECT_FALSE(ExpFI->Inline.hasValue()); 1538 StringRef MethodName = GR->getString(ExpFI->Name); 1539 EXPECT_EQ(MethodName, "Foo::dump"); 1540 } 1541 1542 TEST(GSYMTest, TestDWARFTextRanges) { 1543 // Linkers don't understand DWARF, they just like to concatenate and 1544 // relocate data within the DWARF sections. This means that if a function 1545 // gets dead stripped, and if those functions use an offset as the 1546 // DW_AT_high_pc, we can end up with many functions at address zero. The 1547 // DwarfTransformer allows clients to specify valid .text address ranges 1548 // and any addresses of any functions must fall within those ranges if any 1549 // have been specified. This means that an object file can calcuate the 1550 // address ranges within the binary where code lives and set these ranges 1551 // as constraints in the DwarfTransformer. ObjectFile instances can 1552 // add a address ranges of sections that have executable permissions. This 1553 // keeps bad information from being added to a GSYM file and causing issues 1554 // when symbolicating. 1555 StringRef yamldata = R"( 1556 debug_str: 1557 - '' 1558 - /tmp/main.c 1559 - main 1560 - dead_stripped 1561 - dead_stripped2 1562 debug_abbrev: 1563 - Table: 1564 - Code: 0x00000001 1565 Tag: DW_TAG_compile_unit 1566 Children: DW_CHILDREN_yes 1567 Attributes: 1568 - Attribute: DW_AT_name 1569 Form: DW_FORM_strp 1570 - Attribute: DW_AT_low_pc 1571 Form: DW_FORM_addr 1572 - Attribute: DW_AT_high_pc 1573 Form: DW_FORM_data4 1574 - Attribute: DW_AT_language 1575 Form: DW_FORM_data2 1576 - Code: 0x00000002 1577 Tag: DW_TAG_subprogram 1578 Children: DW_CHILDREN_no 1579 Attributes: 1580 - Attribute: DW_AT_name 1581 Form: DW_FORM_strp 1582 - Attribute: DW_AT_low_pc 1583 Form: DW_FORM_addr 1584 - Attribute: DW_AT_high_pc 1585 Form: DW_FORM_data4 1586 debug_info: 1587 - Version: 4 1588 AddrSize: 8 1589 Entries: 1590 - AbbrCode: 0x00000001 1591 Values: 1592 - Value: 0x0000000000000001 1593 - Value: 0x0000000000001000 1594 - Value: 0x0000000000001000 1595 - Value: 0x0000000000000004 1596 - AbbrCode: 0x00000002 1597 Values: 1598 - Value: 0x000000000000000D 1599 - Value: 0x0000000000001000 1600 - Value: 0x0000000000001000 1601 - AbbrCode: 0x00000002 1602 Values: 1603 - Value: 0x0000000000000012 1604 - Value: 0x0000000000000000 1605 - Value: 0x0000000000000100 1606 - AbbrCode: 0x00000002 1607 Values: 1608 - Value: 0x0000000000000020 1609 - Value: 0x0000000000000000 1610 - Value: 0x0000000000000040 1611 - AbbrCode: 0x00000000 1612 )"; 1613 auto ErrOrSections = DWARFYAML::emitDebugSections(yamldata); 1614 ASSERT_THAT_EXPECTED(ErrOrSections, Succeeded()); 1615 std::unique_ptr<DWARFContext> DwarfContext = 1616 DWARFContext::create(*ErrOrSections, 8); 1617 ASSERT_TRUE(DwarfContext.get() != nullptr); 1618 auto &OS = llvm::nulls(); 1619 GsymCreator GC; 1620 DwarfTransformer DT(*DwarfContext, OS, GC); 1621 // Only allow addresses between [0x1000 - 0x2000) to be linked into the 1622 // GSYM. 1623 AddressRanges TextRanges; 1624 TextRanges.insert(AddressRange(0x1000, 0x2000)); 1625 GC.SetValidTextRanges(TextRanges); 1626 const uint32_t ThreadCount = 1; 1627 ASSERT_THAT_ERROR(DT.convert(ThreadCount), Succeeded()); 1628 ASSERT_THAT_ERROR(GC.finalize(OS), Succeeded()); 1629 SmallString<512> Str; 1630 raw_svector_ostream OutStrm(Str); 1631 const auto ByteOrder = support::endian::system_endianness(); 1632 FileWriter FW(OutStrm, ByteOrder); 1633 ASSERT_THAT_ERROR(GC.encode(FW), Succeeded()); 1634 Expected<GsymReader> GR = GsymReader::copyBuffer(OutStrm.str()); 1635 ASSERT_THAT_EXPECTED(GR, Succeeded()); 1636 // There should only be one function in our GSYM. 1637 EXPECT_EQ(GR->getNumAddresses(), 1u); 1638 auto ExpFI = GR->getFunctionInfo(0x1000); 1639 ASSERT_THAT_EXPECTED(ExpFI, Succeeded()); 1640 ASSERT_EQ(ExpFI->Range, AddressRange(0x1000, 0x2000)); 1641 EXPECT_FALSE(ExpFI->OptLineTable.hasValue()); 1642 EXPECT_FALSE(ExpFI->Inline.hasValue()); 1643 StringRef MethodName = GR->getString(ExpFI->Name); 1644 EXPECT_EQ(MethodName, "main"); 1645 } 1646 1647 TEST(GSYMTest, TestDWARFInlineInfo) { 1648 // Make sure we parse the line table and inline information correctly from 1649 // DWARF. 1650 StringRef yamldata = R"( 1651 debug_str: 1652 - '' 1653 - /tmp/main.c 1654 - main 1655 - inline1 1656 debug_abbrev: 1657 - Table: 1658 - Code: 0x00000001 1659 Tag: DW_TAG_compile_unit 1660 Children: DW_CHILDREN_yes 1661 Attributes: 1662 - Attribute: DW_AT_name 1663 Form: DW_FORM_strp 1664 - Attribute: DW_AT_low_pc 1665 Form: DW_FORM_addr 1666 - Attribute: DW_AT_high_pc 1667 Form: DW_FORM_data4 1668 - Attribute: DW_AT_language 1669 Form: DW_FORM_data2 1670 - Attribute: DW_AT_stmt_list 1671 Form: DW_FORM_sec_offset 1672 - Code: 0x00000002 1673 Tag: DW_TAG_subprogram 1674 Children: DW_CHILDREN_yes 1675 Attributes: 1676 - Attribute: DW_AT_name 1677 Form: DW_FORM_strp 1678 - Attribute: DW_AT_low_pc 1679 Form: DW_FORM_addr 1680 - Attribute: DW_AT_high_pc 1681 Form: DW_FORM_data4 1682 - Code: 0x00000003 1683 Tag: DW_TAG_inlined_subroutine 1684 Children: DW_CHILDREN_no 1685 Attributes: 1686 - Attribute: DW_AT_name 1687 Form: DW_FORM_strp 1688 - Attribute: DW_AT_low_pc 1689 Form: DW_FORM_addr 1690 - Attribute: DW_AT_high_pc 1691 Form: DW_FORM_data4 1692 - Attribute: DW_AT_call_file 1693 Form: DW_FORM_data4 1694 - Attribute: DW_AT_call_line 1695 Form: DW_FORM_data4 1696 debug_info: 1697 - Version: 4 1698 AddrSize: 8 1699 Entries: 1700 - AbbrCode: 0x00000001 1701 Values: 1702 - Value: 0x0000000000000001 1703 - Value: 0x0000000000001000 1704 - Value: 0x0000000000001000 1705 - Value: 0x0000000000000004 1706 - Value: 0x0000000000000000 1707 - AbbrCode: 0x00000002 1708 Values: 1709 - Value: 0x000000000000000D 1710 - Value: 0x0000000000001000 1711 - Value: 0x0000000000001000 1712 - AbbrCode: 0x00000003 1713 Values: 1714 - Value: 0x0000000000000012 1715 - Value: 0x0000000000001100 1716 - Value: 0x0000000000000100 1717 - Value: 0x0000000000000001 1718 - Value: 0x000000000000000A 1719 - AbbrCode: 0x00000000 1720 - AbbrCode: 0x00000000 1721 debug_line: 1722 - Length: 96 1723 Version: 2 1724 PrologueLength: 46 1725 MinInstLength: 1 1726 DefaultIsStmt: 1 1727 LineBase: 251 1728 LineRange: 14 1729 OpcodeBase: 13 1730 StandardOpcodeLengths: [ 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1 ] 1731 IncludeDirs: 1732 - /tmp 1733 Files: 1734 - Name: main.c 1735 DirIdx: 1 1736 ModTime: 0 1737 Length: 0 1738 - Name: inline.h 1739 DirIdx: 1 1740 ModTime: 0 1741 Length: 0 1742 Opcodes: 1743 - Opcode: DW_LNS_extended_op 1744 ExtLen: 9 1745 SubOpcode: DW_LNE_set_address 1746 Data: 4096 1747 - Opcode: DW_LNS_advance_line 1748 SData: 9 1749 Data: 4096 1750 - Opcode: DW_LNS_copy 1751 Data: 4096 1752 - Opcode: DW_LNS_advance_pc 1753 Data: 256 1754 - Opcode: DW_LNS_set_file 1755 Data: 2 1756 - Opcode: DW_LNS_advance_line 1757 SData: 10 1758 Data: 2 1759 - Opcode: DW_LNS_copy 1760 Data: 2 1761 - Opcode: DW_LNS_advance_pc 1762 Data: 128 1763 - Opcode: DW_LNS_advance_line 1764 SData: 1 1765 Data: 128 1766 - Opcode: DW_LNS_copy 1767 Data: 128 1768 - Opcode: DW_LNS_advance_pc 1769 Data: 128 1770 - Opcode: DW_LNS_set_file 1771 Data: 1 1772 - Opcode: DW_LNS_advance_line 1773 SData: -10 1774 Data: 1 1775 - Opcode: DW_LNS_copy 1776 Data: 1 1777 - Opcode: DW_LNS_advance_pc 1778 Data: 3584 1779 - Opcode: DW_LNS_advance_line 1780 SData: 1 1781 Data: 3584 1782 - Opcode: DW_LNS_extended_op 1783 ExtLen: 1 1784 SubOpcode: DW_LNE_end_sequence 1785 Data: 3584 1786 )"; 1787 auto ErrOrSections = DWARFYAML::emitDebugSections(yamldata); 1788 ASSERT_THAT_EXPECTED(ErrOrSections, Succeeded()); 1789 std::unique_ptr<DWARFContext> DwarfContext = 1790 DWARFContext::create(*ErrOrSections, 8); 1791 ASSERT_TRUE(DwarfContext.get() != nullptr); 1792 auto &OS = llvm::nulls(); 1793 GsymCreator GC; 1794 DwarfTransformer DT(*DwarfContext, OS, GC); 1795 const uint32_t ThreadCount = 1; 1796 ASSERT_THAT_ERROR(DT.convert(ThreadCount), Succeeded()); 1797 ASSERT_THAT_ERROR(GC.finalize(OS), Succeeded()); 1798 SmallString<512> Str; 1799 raw_svector_ostream OutStrm(Str); 1800 const auto ByteOrder = support::endian::system_endianness(); 1801 FileWriter FW(OutStrm, ByteOrder); 1802 ASSERT_THAT_ERROR(GC.encode(FW), Succeeded()); 1803 Expected<GsymReader> GR = GsymReader::copyBuffer(OutStrm.str()); 1804 ASSERT_THAT_EXPECTED(GR, Succeeded()); 1805 // There should only be one function in our GSYM. 1806 EXPECT_EQ(GR->getNumAddresses(), 1u); 1807 auto ExpFI = GR->getFunctionInfo(0x1000); 1808 ASSERT_THAT_EXPECTED(ExpFI, Succeeded()); 1809 ASSERT_EQ(ExpFI->Range, AddressRange(0x1000, 0x2000)); 1810 EXPECT_TRUE(ExpFI->OptLineTable.hasValue()); 1811 EXPECT_TRUE(ExpFI->Inline.hasValue()); 1812 StringRef MethodName = GR->getString(ExpFI->Name); 1813 EXPECT_EQ(MethodName, "main"); 1814 1815 // Verify inline info is correct when doing lookups. 1816 auto LR = GR->lookup(0x1000); 1817 ASSERT_THAT_EXPECTED(LR, Succeeded()); 1818 EXPECT_THAT(LR->Locations, 1819 testing::ElementsAre(SourceLocation{"main", "/tmp", "main.c", 10})); 1820 LR = GR->lookup(0x1100-1); 1821 ASSERT_THAT_EXPECTED(LR, Succeeded()); 1822 EXPECT_THAT(LR->Locations, 1823 testing::ElementsAre(SourceLocation{"main", "/tmp", "main.c", 10, 255})); 1824 1825 LR = GR->lookup(0x1100); 1826 ASSERT_THAT_EXPECTED(LR, Succeeded()); 1827 EXPECT_THAT(LR->Locations, 1828 testing::ElementsAre(SourceLocation{"inline1", "/tmp", "inline.h", 20}, 1829 SourceLocation{"main", "/tmp", "main.c", 10, 256})); 1830 LR = GR->lookup(0x1180-1); 1831 ASSERT_THAT_EXPECTED(LR, Succeeded()); 1832 EXPECT_THAT(LR->Locations, 1833 testing::ElementsAre(SourceLocation{"inline1", "/tmp", "inline.h", 20, 127}, 1834 SourceLocation{"main", "/tmp", "main.c", 10, 383})); 1835 LR = GR->lookup(0x1180); 1836 ASSERT_THAT_EXPECTED(LR, Succeeded()); 1837 EXPECT_THAT(LR->Locations, 1838 testing::ElementsAre(SourceLocation{"inline1", "/tmp", "inline.h", 21, 128}, 1839 SourceLocation{"main", "/tmp", "main.c", 10, 384})); 1840 LR = GR->lookup(0x1200-1); 1841 ASSERT_THAT_EXPECTED(LR, Succeeded()); 1842 EXPECT_THAT(LR->Locations, 1843 testing::ElementsAre(SourceLocation{"inline1", "/tmp", "inline.h", 21, 255}, 1844 SourceLocation{"main", "/tmp", "main.c", 10, 511})); 1845 LR = GR->lookup(0x1200); 1846 ASSERT_THAT_EXPECTED(LR, Succeeded()); 1847 EXPECT_THAT(LR->Locations, 1848 testing::ElementsAre(SourceLocation{"main", "/tmp", "main.c", 11, 512})); 1849 } 1850 1851 1852 TEST(GSYMTest, TestDWARFNoLines) { 1853 // Check that if a DW_TAG_subprogram doesn't have line table entries that 1854 // we fall back and use the DW_AT_decl_file and DW_AT_decl_line to at least 1855 // point to the function definition. This DWARF file has 4 functions: 1856 // "lines_no_decl": has line table entries, no DW_AT_decl_file/line attrs. 1857 // "lines_with_decl": has line table entries and has DW_AT_decl_file/line, 1858 // make sure we don't use DW_AT_decl_file/line and make 1859 // sure there is a line table. 1860 // "no_lines_no_decl": no line table entries and no DW_AT_decl_file/line, 1861 // make sure there is no line table for this function. 1862 // "no_lines_with_decl": no line table and has DW_AT_decl_file/line, make 1863 // sure we have one line table entry that starts at 1864 // the function start address and the decl file and 1865 // line. 1866 // 1867 // 0x0000000b: DW_TAG_compile_unit 1868 // DW_AT_name ("/tmp/main.c") 1869 // DW_AT_low_pc (0x0000000000001000) 1870 // DW_AT_high_pc (0x0000000000002000) 1871 // DW_AT_language (DW_LANG_C_plus_plus) 1872 // DW_AT_stmt_list (0x00000000) 1873 // 1874 // 0x00000022: DW_TAG_subprogram 1875 // DW_AT_name ("lines_no_decl") 1876 // DW_AT_low_pc (0x0000000000001000) 1877 // DW_AT_high_pc (0x0000000000002000) 1878 // 1879 // 0x00000033: DW_TAG_subprogram 1880 // DW_AT_name ("lines_with_decl") 1881 // DW_AT_low_pc (0x0000000000002000) 1882 // DW_AT_high_pc (0x0000000000003000) 1883 // DW_AT_decl_file ("/tmp/main.c") 1884 // DW_AT_decl_line (20) 1885 // 1886 // 0x00000046: DW_TAG_subprogram 1887 // DW_AT_name ("no_lines_no_decl") 1888 // DW_AT_low_pc (0x0000000000003000) 1889 // DW_AT_high_pc (0x0000000000004000) 1890 // 1891 // 0x00000057: DW_TAG_subprogram 1892 // DW_AT_name ("no_lines_with_decl") 1893 // DW_AT_low_pc (0x0000000000004000) 1894 // DW_AT_high_pc (0x0000000000005000) 1895 // DW_AT_decl_file ("/tmp/main.c") 1896 // DW_AT_decl_line (40) 1897 // 1898 // 0x0000006a: NULL 1899 1900 StringRef yamldata = R"( 1901 debug_str: 1902 - '' 1903 - '/tmp/main.c' 1904 - lines_no_decl 1905 - lines_with_decl 1906 - no_lines_no_decl 1907 - no_lines_with_decl 1908 debug_abbrev: 1909 - Table: 1910 - Code: 0x00000001 1911 Tag: DW_TAG_compile_unit 1912 Children: DW_CHILDREN_yes 1913 Attributes: 1914 - Attribute: DW_AT_name 1915 Form: DW_FORM_strp 1916 - Attribute: DW_AT_low_pc 1917 Form: DW_FORM_addr 1918 - Attribute: DW_AT_high_pc 1919 Form: DW_FORM_data4 1920 - Attribute: DW_AT_language 1921 Form: DW_FORM_data2 1922 - Attribute: DW_AT_stmt_list 1923 Form: DW_FORM_sec_offset 1924 - Code: 0x00000002 1925 Tag: DW_TAG_subprogram 1926 Children: DW_CHILDREN_no 1927 Attributes: 1928 - Attribute: DW_AT_name 1929 Form: DW_FORM_strp 1930 - Attribute: DW_AT_low_pc 1931 Form: DW_FORM_addr 1932 - Attribute: DW_AT_high_pc 1933 Form: DW_FORM_data4 1934 - Code: 0x00000003 1935 Tag: DW_TAG_subprogram 1936 Children: DW_CHILDREN_no 1937 Attributes: 1938 - Attribute: DW_AT_name 1939 Form: DW_FORM_strp 1940 - Attribute: DW_AT_low_pc 1941 Form: DW_FORM_addr 1942 - Attribute: DW_AT_high_pc 1943 Form: DW_FORM_data4 1944 - Attribute: DW_AT_decl_file 1945 Form: DW_FORM_data1 1946 - Attribute: DW_AT_decl_line 1947 Form: DW_FORM_data1 1948 debug_info: 1949 - Version: 4 1950 AddrSize: 8 1951 Entries: 1952 - AbbrCode: 0x00000001 1953 Values: 1954 - Value: 0x0000000000000001 1955 - Value: 0x0000000000001000 1956 - Value: 0x0000000000001000 1957 - Value: 0x0000000000000004 1958 - Value: 0x0000000000000000 1959 - AbbrCode: 0x00000002 1960 Values: 1961 - Value: 0x000000000000000D 1962 - Value: 0x0000000000001000 1963 - Value: 0x0000000000001000 1964 - AbbrCode: 0x00000003 1965 Values: 1966 - Value: 0x000000000000001B 1967 - Value: 0x0000000000002000 1968 - Value: 0x0000000000001000 1969 - Value: 0x0000000000000001 1970 - Value: 0x0000000000000014 1971 - AbbrCode: 0x00000002 1972 Values: 1973 - Value: 0x000000000000002B 1974 - Value: 0x0000000000003000 1975 - Value: 0x0000000000001000 1976 - AbbrCode: 0x00000003 1977 Values: 1978 - Value: 0x000000000000003C 1979 - Value: 0x0000000000004000 1980 - Value: 0x0000000000001000 1981 - Value: 0x0000000000000001 1982 - Value: 0x0000000000000028 1983 - AbbrCode: 0x00000000 1984 debug_line: 1985 - Length: 92 1986 Version: 2 1987 PrologueLength: 34 1988 MinInstLength: 1 1989 DefaultIsStmt: 1 1990 LineBase: 251 1991 LineRange: 14 1992 OpcodeBase: 13 1993 StandardOpcodeLengths: [ 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1 ] 1994 IncludeDirs: 1995 - '/tmp' 1996 Files: 1997 - Name: main.c 1998 DirIdx: 1 1999 ModTime: 0 2000 Length: 0 2001 Opcodes: 2002 - Opcode: DW_LNS_extended_op 2003 ExtLen: 9 2004 SubOpcode: DW_LNE_set_address 2005 Data: 4096 2006 - Opcode: DW_LNS_advance_line 2007 SData: 10 2008 Data: 0 2009 - Opcode: DW_LNS_copy 2010 Data: 0 2011 - Opcode: DW_LNS_advance_pc 2012 Data: 512 2013 - Opcode: DW_LNS_advance_line 2014 SData: 1 2015 Data: 0 2016 - Opcode: DW_LNS_copy 2017 Data: 0 2018 - Opcode: DW_LNS_advance_pc 2019 Data: 3584 2020 - Opcode: DW_LNS_extended_op 2021 ExtLen: 1 2022 SubOpcode: DW_LNE_end_sequence 2023 Data: 0 2024 - Opcode: DW_LNS_extended_op 2025 ExtLen: 9 2026 SubOpcode: DW_LNE_set_address 2027 Data: 8192 2028 - Opcode: DW_LNS_advance_line 2029 SData: 20 2030 Data: 0 2031 - Opcode: DW_LNS_copy 2032 Data: 0 2033 - Opcode: DW_LNS_advance_pc 2034 Data: 512 2035 - Opcode: DW_LNS_advance_line 2036 SData: 1 2037 Data: 0 2038 - Opcode: DW_LNS_copy 2039 Data: 0 2040 - Opcode: DW_LNS_advance_pc 2041 Data: 3584 2042 - Opcode: DW_LNS_extended_op 2043 ExtLen: 1 2044 SubOpcode: DW_LNE_end_sequence 2045 Data: 0 2046 )"; 2047 auto ErrOrSections = DWARFYAML::emitDebugSections(yamldata); 2048 ASSERT_THAT_EXPECTED(ErrOrSections, Succeeded()); 2049 std::unique_ptr<DWARFContext> DwarfContext = 2050 DWARFContext::create(*ErrOrSections, 8); 2051 ASSERT_TRUE(DwarfContext.get() != nullptr); 2052 auto &OS = llvm::nulls(); 2053 GsymCreator GC; 2054 DwarfTransformer DT(*DwarfContext, OS, GC); 2055 const uint32_t ThreadCount = 1; 2056 ASSERT_THAT_ERROR(DT.convert(ThreadCount), Succeeded()); 2057 ASSERT_THAT_ERROR(GC.finalize(OS), Succeeded()); 2058 SmallString<512> Str; 2059 raw_svector_ostream OutStrm(Str); 2060 const auto ByteOrder = support::endian::system_endianness(); 2061 FileWriter FW(OutStrm, ByteOrder); 2062 ASSERT_THAT_ERROR(GC.encode(FW), Succeeded()); 2063 Expected<GsymReader> GR = GsymReader::copyBuffer(OutStrm.str()); 2064 ASSERT_THAT_EXPECTED(GR, Succeeded()); 2065 2066 EXPECT_EQ(GR->getNumAddresses(), 4u); 2067 2068 auto ExpFI = GR->getFunctionInfo(0x1000); 2069 ASSERT_THAT_EXPECTED(ExpFI, Succeeded()); 2070 ASSERT_EQ(ExpFI->Range, AddressRange(0x1000, 0x2000)); 2071 EXPECT_TRUE(ExpFI->OptLineTable.hasValue()); 2072 StringRef MethodName = GR->getString(ExpFI->Name); 2073 EXPECT_EQ(MethodName, "lines_no_decl"); 2074 // Make sure have two line table entries and that get the first line entry 2075 // correct. 2076 EXPECT_EQ(ExpFI->OptLineTable->size(), 2u); 2077 EXPECT_EQ(ExpFI->OptLineTable->first()->Addr, 0x1000u); 2078 EXPECT_EQ(ExpFI->OptLineTable->first()->Line, 11u); 2079 2080 ExpFI = GR->getFunctionInfo(0x2000); 2081 ASSERT_THAT_EXPECTED(ExpFI, Succeeded()); 2082 ASSERT_EQ(ExpFI->Range, AddressRange(0x2000, 0x3000)); 2083 EXPECT_TRUE(ExpFI->OptLineTable.hasValue()); 2084 MethodName = GR->getString(ExpFI->Name); 2085 EXPECT_EQ(MethodName, "lines_with_decl"); 2086 // Make sure have two line table entries and that we don't use line 20 2087 // from the DW_AT_decl_file/line as a line table entry. 2088 EXPECT_EQ(ExpFI->OptLineTable->size(), 2u); 2089 EXPECT_EQ(ExpFI->OptLineTable->first()->Addr, 0x2000u); 2090 EXPECT_EQ(ExpFI->OptLineTable->first()->Line, 21u); 2091 2092 ExpFI = GR->getFunctionInfo(0x3000); 2093 ASSERT_THAT_EXPECTED(ExpFI, Succeeded()); 2094 ASSERT_EQ(ExpFI->Range, AddressRange(0x3000, 0x4000)); 2095 // Make sure we have no line table. 2096 EXPECT_FALSE(ExpFI->OptLineTable.hasValue()); 2097 MethodName = GR->getString(ExpFI->Name); 2098 EXPECT_EQ(MethodName, "no_lines_no_decl"); 2099 2100 ExpFI = GR->getFunctionInfo(0x4000); 2101 ASSERT_THAT_EXPECTED(ExpFI, Succeeded()); 2102 ASSERT_EQ(ExpFI->Range, AddressRange(0x4000, 0x5000)); 2103 EXPECT_TRUE(ExpFI->OptLineTable.hasValue()); 2104 MethodName = GR->getString(ExpFI->Name); 2105 EXPECT_EQ(MethodName, "no_lines_with_decl"); 2106 // Make sure we have one line table entry that uses the DW_AT_decl_file/line 2107 // as the one and only line entry. 2108 EXPECT_EQ(ExpFI->OptLineTable->size(), 1u); 2109 EXPECT_EQ(ExpFI->OptLineTable->first()->Addr, 0x4000u); 2110 EXPECT_EQ(ExpFI->OptLineTable->first()->Line, 40u); 2111 } 2112 2113 2114 TEST(GSYMTest, TestDWARFDeadStripAddr4) { 2115 // Check that various techniques that compilers use for dead code stripping 2116 // work for 4 byte addresses. Make sure we keep the good functions and 2117 // strip any functions whose name starts with "stripped". 2118 // 2119 // 1 - Compilers might set the low PC to -1 (UINT32_MAX) for compile unit 2120 // with 4 byte addresses ("stripped1") 2121 // 2 - Set the low and high PC to the same value ("stripped2") 2122 // 3 - Have the high PC lower than the low PC ("stripped3") 2123 // 2124 // 0x0000000b: DW_TAG_compile_unit 2125 // DW_AT_name ("/tmp/main.c") 2126 // DW_AT_low_pc (0x0000000000001000) 2127 // DW_AT_high_pc (0x0000000000002000) 2128 // DW_AT_language (DW_LANG_C_plus_plus) 2129 // 2130 // 0x0000001a: DW_TAG_subprogram 2131 // DW_AT_name ("main") 2132 // DW_AT_low_pc (0x0000000000001000) 2133 // DW_AT_high_pc (0x0000000000002000) 2134 // 2135 // 0x00000027: DW_TAG_subprogram 2136 // DW_AT_name ("stripped1") 2137 // DW_AT_low_pc (0x00000000ffffffff) 2138 // DW_AT_high_pc (0x0000000100000000) 2139 // 2140 // 0x00000034: DW_TAG_subprogram 2141 // DW_AT_name ("stripped2") 2142 // DW_AT_low_pc (0x0000000000003000) 2143 // DW_AT_high_pc (0x0000000000003000) 2144 // 2145 // 0x00000041: DW_TAG_subprogram 2146 // DW_AT_name ("stripped3") 2147 // DW_AT_low_pc (0x0000000000004000) 2148 // DW_AT_high_pc (0x0000000000003fff) 2149 // 2150 // 0x0000004e: NULL 2151 2152 StringRef yamldata = R"( 2153 debug_str: 2154 - '' 2155 - '/tmp/main.c' 2156 - main 2157 - stripped1 2158 - stripped2 2159 - stripped3 2160 debug_abbrev: 2161 - Table: 2162 - Code: 0x00000001 2163 Tag: DW_TAG_compile_unit 2164 Children: DW_CHILDREN_yes 2165 Attributes: 2166 - Attribute: DW_AT_name 2167 Form: DW_FORM_strp 2168 - Attribute: DW_AT_low_pc 2169 Form: DW_FORM_addr 2170 - Attribute: DW_AT_high_pc 2171 Form: DW_FORM_data4 2172 - Attribute: DW_AT_language 2173 Form: DW_FORM_data2 2174 - Code: 0x00000002 2175 Tag: DW_TAG_subprogram 2176 Children: DW_CHILDREN_no 2177 Attributes: 2178 - Attribute: DW_AT_name 2179 Form: DW_FORM_strp 2180 - Attribute: DW_AT_low_pc 2181 Form: DW_FORM_addr 2182 - Attribute: DW_AT_high_pc 2183 Form: DW_FORM_data4 2184 - Code: 0x00000003 2185 Tag: DW_TAG_subprogram 2186 Children: DW_CHILDREN_no 2187 Attributes: 2188 - Attribute: DW_AT_name 2189 Form: DW_FORM_strp 2190 - Attribute: DW_AT_low_pc 2191 Form: DW_FORM_addr 2192 - Attribute: DW_AT_high_pc 2193 Form: DW_FORM_addr 2194 debug_info: 2195 - Version: 4 2196 AddrSize: 4 2197 Entries: 2198 - AbbrCode: 0x00000001 2199 Values: 2200 - Value: 0x0000000000000001 2201 - Value: 0x0000000000001000 2202 - Value: 0x0000000000001000 2203 - Value: 0x0000000000000004 2204 - AbbrCode: 0x00000002 2205 Values: 2206 - Value: 0x000000000000000D 2207 - Value: 0x0000000000001000 2208 - Value: 0x0000000000001000 2209 - AbbrCode: 0x00000002 2210 Values: 2211 - Value: 0x0000000000000012 2212 - Value: 0x00000000FFFFFFFF 2213 - Value: 0x0000000000000001 2214 - AbbrCode: 0x00000003 2215 Values: 2216 - Value: 0x000000000000001C 2217 - Value: 0x0000000000003000 2218 - Value: 0x0000000000003000 2219 - AbbrCode: 0x00000003 2220 Values: 2221 - Value: 0x0000000000000026 2222 - Value: 0x0000000000004000 2223 - Value: 0x0000000000003FFF 2224 - AbbrCode: 0x00000000 2225 )"; 2226 auto ErrOrSections = DWARFYAML::emitDebugSections(yamldata); 2227 ASSERT_THAT_EXPECTED(ErrOrSections, Succeeded()); 2228 std::unique_ptr<DWARFContext> DwarfContext = 2229 DWARFContext::create(*ErrOrSections, 4); 2230 ASSERT_TRUE(DwarfContext.get() != nullptr); 2231 auto &OS = llvm::nulls(); 2232 GsymCreator GC; 2233 DwarfTransformer DT(*DwarfContext, OS, GC); 2234 const uint32_t ThreadCount = 1; 2235 ASSERT_THAT_ERROR(DT.convert(ThreadCount), Succeeded()); 2236 ASSERT_THAT_ERROR(GC.finalize(OS), Succeeded()); 2237 SmallString<512> Str; 2238 raw_svector_ostream OutStrm(Str); 2239 const auto ByteOrder = support::endian::system_endianness(); 2240 FileWriter FW(OutStrm, ByteOrder); 2241 ASSERT_THAT_ERROR(GC.encode(FW), Succeeded()); 2242 Expected<GsymReader> GR = GsymReader::copyBuffer(OutStrm.str()); 2243 ASSERT_THAT_EXPECTED(GR, Succeeded()); 2244 2245 // Test that the only function that made it was the "main" function. 2246 EXPECT_EQ(GR->getNumAddresses(), 1u); 2247 auto ExpFI = GR->getFunctionInfo(0x1000); 2248 ASSERT_THAT_EXPECTED(ExpFI, Succeeded()); 2249 ASSERT_EQ(ExpFI->Range, AddressRange(0x1000, 0x2000)); 2250 StringRef MethodName = GR->getString(ExpFI->Name); 2251 EXPECT_EQ(MethodName, "main"); 2252 } 2253 2254 TEST(GSYMTest, TestDWARFDeadStripAddr8) { 2255 // Check that various techniques that compilers use for dead code stripping 2256 // work for 4 byte addresses. Make sure we keep the good functions and 2257 // strip any functions whose name starts with "stripped". 2258 // 2259 // 1 - Compilers might set the low PC to -1 (UINT64_MAX) for compile unit 2260 // with 8 byte addresses ("stripped1") 2261 // 2 - Set the low and high PC to the same value ("stripped2") 2262 // 3 - Have the high PC lower than the low PC ("stripped3") 2263 // 2264 // 0x0000000b: DW_TAG_compile_unit 2265 // DW_AT_name ("/tmp/main.c") 2266 // DW_AT_low_pc (0x0000000000001000) 2267 // DW_AT_high_pc (0x0000000000002000) 2268 // DW_AT_language (DW_LANG_C_plus_plus) 2269 // 2270 // 0x0000001e: DW_TAG_subprogram 2271 // DW_AT_name ("main") 2272 // DW_AT_low_pc (0x0000000000001000) 2273 // DW_AT_high_pc (0x0000000000002000) 2274 // 2275 // 0x0000002f: DW_TAG_subprogram 2276 // DW_AT_name ("stripped1") 2277 // DW_AT_low_pc (0xffffffffffffffff) 2278 // DW_AT_high_pc (0x0000000000000000) 2279 // 2280 // 0x00000040: DW_TAG_subprogram 2281 // DW_AT_name ("stripped2") 2282 // DW_AT_low_pc (0x0000000000003000) 2283 // DW_AT_high_pc (0x0000000000003000) 2284 // 2285 // 0x00000055: DW_TAG_subprogram 2286 // DW_AT_name ("stripped3") 2287 // DW_AT_low_pc (0x0000000000004000) 2288 // DW_AT_high_pc (0x0000000000003fff) 2289 // 2290 // 0x0000006a: NULL 2291 2292 StringRef yamldata = R"( 2293 debug_str: 2294 - '' 2295 - '/tmp/main.c' 2296 - main 2297 - stripped1 2298 - stripped2 2299 - stripped3 2300 debug_abbrev: 2301 - Table: 2302 - Code: 0x00000001 2303 Tag: DW_TAG_compile_unit 2304 Children: DW_CHILDREN_yes 2305 Attributes: 2306 - Attribute: DW_AT_name 2307 Form: DW_FORM_strp 2308 - Attribute: DW_AT_low_pc 2309 Form: DW_FORM_addr 2310 - Attribute: DW_AT_high_pc 2311 Form: DW_FORM_data4 2312 - Attribute: DW_AT_language 2313 Form: DW_FORM_data2 2314 - Code: 0x00000002 2315 Tag: DW_TAG_subprogram 2316 Children: DW_CHILDREN_no 2317 Attributes: 2318 - Attribute: DW_AT_name 2319 Form: DW_FORM_strp 2320 - Attribute: DW_AT_low_pc 2321 Form: DW_FORM_addr 2322 - Attribute: DW_AT_high_pc 2323 Form: DW_FORM_data4 2324 - Code: 0x00000003 2325 Tag: DW_TAG_subprogram 2326 Children: DW_CHILDREN_no 2327 Attributes: 2328 - Attribute: DW_AT_name 2329 Form: DW_FORM_strp 2330 - Attribute: DW_AT_low_pc 2331 Form: DW_FORM_addr 2332 - Attribute: DW_AT_high_pc 2333 Form: DW_FORM_addr 2334 debug_info: 2335 - Version: 4 2336 AddrSize: 8 2337 Entries: 2338 - AbbrCode: 0x00000001 2339 Values: 2340 - Value: 0x0000000000000001 2341 - Value: 0x0000000000001000 2342 - Value: 0x0000000000001000 2343 - Value: 0x0000000000000004 2344 - AbbrCode: 0x00000002 2345 Values: 2346 - Value: 0x000000000000000D 2347 - Value: 0x0000000000001000 2348 - Value: 0x0000000000001000 2349 - AbbrCode: 0x00000002 2350 Values: 2351 - Value: 0x0000000000000012 2352 - Value: 0xFFFFFFFFFFFFFFFF 2353 - Value: 0x0000000000000001 2354 - AbbrCode: 0x00000003 2355 Values: 2356 - Value: 0x000000000000001C 2357 - Value: 0x0000000000003000 2358 - Value: 0x0000000000003000 2359 - AbbrCode: 0x00000003 2360 Values: 2361 - Value: 0x0000000000000026 2362 - Value: 0x0000000000004000 2363 - Value: 0x0000000000003FFF 2364 - AbbrCode: 0x00000000 2365 )"; 2366 auto ErrOrSections = DWARFYAML::emitDebugSections(yamldata); 2367 ASSERT_THAT_EXPECTED(ErrOrSections, Succeeded()); 2368 std::unique_ptr<DWARFContext> DwarfContext = 2369 DWARFContext::create(*ErrOrSections, 8); 2370 ASSERT_TRUE(DwarfContext.get() != nullptr); 2371 auto &OS = llvm::nulls(); 2372 GsymCreator GC; 2373 DwarfTransformer DT(*DwarfContext, OS, GC); 2374 const uint32_t ThreadCount = 1; 2375 ASSERT_THAT_ERROR(DT.convert(ThreadCount), Succeeded()); 2376 ASSERT_THAT_ERROR(GC.finalize(OS), Succeeded()); 2377 SmallString<512> Str; 2378 raw_svector_ostream OutStrm(Str); 2379 const auto ByteOrder = support::endian::system_endianness(); 2380 FileWriter FW(OutStrm, ByteOrder); 2381 ASSERT_THAT_ERROR(GC.encode(FW), Succeeded()); 2382 Expected<GsymReader> GR = GsymReader::copyBuffer(OutStrm.str()); 2383 ASSERT_THAT_EXPECTED(GR, Succeeded()); 2384 2385 // Test that the only function that made it was the "main" function. 2386 EXPECT_EQ(GR->getNumAddresses(), 1u); 2387 auto ExpFI = GR->getFunctionInfo(0x1000); 2388 ASSERT_THAT_EXPECTED(ExpFI, Succeeded()); 2389 ASSERT_EQ(ExpFI->Range, AddressRange(0x1000, 0x2000)); 2390 StringRef MethodName = GR->getString(ExpFI->Name); 2391 EXPECT_EQ(MethodName, "main"); 2392 } 2393 2394 TEST(GSYMTest, TestGsymCreatorMultipleSymbolsWithNoSize) { 2395 // Multiple symbols at the same address with zero size were being emitted 2396 // instead of being combined into a single entry. This function tests to make 2397 // sure we only get one symbol. 2398 uint8_t UUID[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}; 2399 GsymCreator GC; 2400 GC.setUUID(UUID); 2401 constexpr uint64_t BaseAddr = 0x1000; 2402 constexpr uint8_t AddrOffSize = 1; 2403 const uint32_t Func1Name = GC.insertString("foo"); 2404 const uint32_t Func2Name = GC.insertString("bar"); 2405 GC.addFunctionInfo(FunctionInfo(BaseAddr, 0, Func1Name)); 2406 GC.addFunctionInfo(FunctionInfo(BaseAddr, 0, Func2Name)); 2407 Error Err = GC.finalize(llvm::nulls()); 2408 ASSERT_FALSE(Err); 2409 TestEncodeDecode(GC, llvm::support::little, GSYM_VERSION, AddrOffSize, 2410 BaseAddr, 2411 1, // NumAddresses 2412 ArrayRef<uint8_t>(UUID)); 2413 TestEncodeDecode(GC, llvm::support::big, GSYM_VERSION, AddrOffSize, BaseAddr, 2414 1, // NumAddresses 2415 ArrayRef<uint8_t>(UUID)); 2416 } 2417