Skip to content

Commit 4348e0e

Browse files
committed
[lldb][NFC] Refactor getUUID functionality
Differential Revision: https://reviews.llvm.org/D90325
1 parent 79657e2 commit 4348e0e

File tree

6 files changed

+42
-101
lines changed

6 files changed

+42
-101
lines changed

lldb/include/lldb/Utility/UUID.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include "llvm/ADT/ArrayRef.h"
1313
#include "llvm/ADT/StringRef.h"
14+
#include "llvm/Support/Endian.h"
1415
#include <stddef.h>
1516
#include <stdint.h>
1617
#include <string>
@@ -23,6 +24,22 @@ class UUID {
2324
public:
2425
UUID() = default;
2526

27+
// Reference:
28+
// https://crashpad.chromium.org/doxygen/structcrashpad_1_1CodeViewRecordPDB70.html
29+
struct CvRecordPdb70 {
30+
struct {
31+
llvm::support::ulittle32_t Data1;
32+
llvm::support::ulittle16_t Data2;
33+
llvm::support::ulittle16_t Data3;
34+
uint8_t Data4[8];
35+
} Uuid;
36+
llvm::support::ulittle32_t Age;
37+
// char PDBFileName[];
38+
};
39+
40+
/// Create a UUID from CvRecordPdb70.
41+
static UUID fromCvRecord(CvRecordPdb70 debug_info);
42+
2643
/// Creates a UUID from the data pointed to by the bytes argument. No special
2744
/// significance is attached to any of the values.
2845
static UUID fromData(const void *bytes, uint32_t num_bytes) {

lldb/source/Plugins/ObjectFile/PDB/ObjectFilePDB.cpp

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -27,43 +27,11 @@ using namespace llvm::codeview;
2727

2828
LLDB_PLUGIN_DEFINE(ObjectFilePDB)
2929

30-
struct CVInfoPdb70 {
31-
// 16-byte GUID
32-
struct _Guid {
33-
llvm::support::ulittle32_t Data1;
34-
llvm::support::ulittle16_t Data2;
35-
llvm::support::ulittle16_t Data3;
36-
uint8_t Data4[8];
37-
} Guid;
38-
39-
llvm::support::ulittle32_t Age;
40-
};
41-
4230
static UUID GetPDBUUID(InfoStream &IS) {
43-
// This part is similar with what has done in ObjectFilePECOFF.
44-
using llvm::support::endian::read16be;
45-
using llvm::support::endian::read32;
46-
using llvm::support::endian::read32be;
47-
48-
GUID guid = IS.getGuid();
49-
const uint8_t *guid_p = guid.Guid;
50-
struct CVInfoPdb70 info;
51-
info.Guid.Data1 = read32be(guid_p);
52-
guid_p += 4;
53-
info.Guid.Data2 = read16be(guid_p);
54-
guid_p += 2;
55-
info.Guid.Data3 = read16be(guid_p);
56-
guid_p += 2;
57-
memcpy(info.Guid.Data4, guid_p, 8);
58-
59-
// Return 20-byte UUID if the Age is not zero
60-
uint32_t age = IS.getAge();
61-
if (age) {
62-
info.Age = read32(&age, llvm::support::big);
63-
return UUID::fromOptionalData(&info, sizeof(info));
64-
}
65-
// Otherwise return 16-byte GUID
66-
return UUID::fromOptionalData(&info.Guid, sizeof(info.Guid));
31+
UUID::CvRecordPdb70 debug_info;
32+
memcpy(&debug_info.Uuid, IS.getGuid().Guid, sizeof(debug_info.Uuid));
33+
debug_info.Age = IS.getAge();
34+
return UUID::fromCvRecord(debug_info);
6735
}
6836

6937
char ObjectFilePDB::ID;

lldb/source/Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.cpp

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -43,45 +43,16 @@ using namespace lldb_private;
4343

4444
LLDB_PLUGIN_DEFINE(ObjectFilePECOFF)
4545

46-
struct CVInfoPdb70 {
47-
// 16-byte GUID
48-
struct _Guid {
49-
llvm::support::ulittle32_t Data1;
50-
llvm::support::ulittle16_t Data2;
51-
llvm::support::ulittle16_t Data3;
52-
uint8_t Data4[8];
53-
} Guid;
54-
55-
llvm::support::ulittle32_t Age;
56-
};
57-
5846
static UUID GetCoffUUID(llvm::object::COFFObjectFile &coff_obj) {
5947
const llvm::codeview::DebugInfo *pdb_info = nullptr;
6048
llvm::StringRef pdb_file;
6149

62-
// This part is similar with what has done in minidump parser.
6350
if (!coff_obj.getDebugPDBInfo(pdb_info, pdb_file) && pdb_info) {
6451
if (pdb_info->PDB70.CVSignature == llvm::OMF::Signature::PDB70) {
65-
using llvm::support::endian::read16be;
66-
using llvm::support::endian::read32be;
67-
68-
const uint8_t *sig = pdb_info->PDB70.Signature;
69-
struct CVInfoPdb70 info;
70-
info.Guid.Data1 = read32be(sig);
71-
sig += 4;
72-
info.Guid.Data2 = read16be(sig);
73-
sig += 2;
74-
info.Guid.Data3 = read16be(sig);
75-
sig += 2;
76-
memcpy(info.Guid.Data4, sig, 8);
77-
78-
// Return 20-byte UUID if the Age is not zero
79-
if (pdb_info->PDB70.Age) {
80-
info.Age = read32be(&pdb_info->PDB70.Age);
81-
return UUID::fromOptionalData(&info, sizeof(info));
82-
}
83-
// Otherwise return 16-byte GUID
84-
return UUID::fromOptionalData(&info.Guid, sizeof(info.Guid));
52+
UUID::CvRecordPdb70 info;
53+
memcpy(&info.Uuid, pdb_info->PDB70.Signature, sizeof(info.Uuid));
54+
info.Age = pdb_info->PDB70.Age;
55+
return UUID::fromCvRecord(info);
8556
}
8657
}
8758

lldb/source/Plugins/Process/minidump/MinidumpParser.cpp

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -62,27 +62,17 @@ UUID MinidumpParser::GetModuleUUID(const minidump::Module *module) {
6262
static_cast<CvSignature>(static_cast<uint32_t>(*signature));
6363

6464
if (cv_signature == CvSignature::Pdb70) {
65-
const CvRecordPdb70 *pdb70_uuid = nullptr;
65+
const UUID::CvRecordPdb70 *pdb70_uuid = nullptr;
6666
Status error = consumeObject(cv_record, pdb70_uuid);
6767
if (error.Fail())
6868
return UUID();
69-
70-
CvRecordPdb70 swapped;
71-
if (!GetArchitecture().GetTriple().isOSBinFormatELF()) {
72-
// LLDB's UUID class treats the data as a sequence of bytes, but breakpad
73-
// interprets it as a sequence of little-endian fields, which it converts
74-
// to big-endian when converting to text. Swap the bytes to big endian so
75-
// that the string representation comes out right.
76-
swapped = *pdb70_uuid;
77-
llvm::sys::swapByteOrder(swapped.Uuid.Data1);
78-
llvm::sys::swapByteOrder(swapped.Uuid.Data2);
79-
llvm::sys::swapByteOrder(swapped.Uuid.Data3);
80-
llvm::sys::swapByteOrder(swapped.Age);
81-
pdb70_uuid = &swapped;
69+
if (GetArchitecture().GetTriple().isOSBinFormatELF()) {
70+
if (pdb70_uuid->Age != 0)
71+
return UUID::fromOptionalData(pdb70_uuid, sizeof(*pdb70_uuid));
72+
return UUID::fromOptionalData(&pdb70_uuid->Uuid,
73+
sizeof(pdb70_uuid->Uuid));
8274
}
83-
if (pdb70_uuid->Age != 0)
84-
return UUID::fromOptionalData(pdb70_uuid, sizeof(*pdb70_uuid));
85-
return UUID::fromOptionalData(&pdb70_uuid->Uuid, sizeof(pdb70_uuid->Uuid));
75+
return UUID::fromCvRecord(*pdb70_uuid);
8676
} else if (cv_signature == CvSignature::ElfBuildId)
8777
return UUID::fromOptionalData(cv_record);
8878

lldb/source/Plugins/Process/minidump/MinidumpTypes.h

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,6 @@ enum class CvSignature : uint32_t {
4040
ElfBuildId = 0x4270454c, // BpEL (Breakpad/Crashpad minidumps)
4141
};
4242

43-
// Reference:
44-
// https://crashpad.chromium.org/doxygen/structcrashpad_1_1CodeViewRecordPDB70.html
45-
struct CvRecordPdb70 {
46-
struct {
47-
llvm::support::ulittle32_t Data1;
48-
llvm::support::ulittle16_t Data2;
49-
llvm::support::ulittle16_t Data3;
50-
uint8_t Data4[8];
51-
} Uuid;
52-
llvm::support::ulittle32_t Age;
53-
// char PDBFileName[];
54-
};
55-
static_assert(sizeof(CvRecordPdb70) == 20,
56-
"sizeof CvRecordPdb70 is not correct!");
57-
5843
enum class MinidumpMiscInfoFlags : uint32_t {
5944
ProcessID = (1 << 0),
6045
ProcessTimes = (1 << 1),

lldb/source/Utility/UUID.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ static inline bool separate(size_t count) {
3535
}
3636
}
3737

38+
UUID UUID::fromCvRecord(UUID::CvRecordPdb70 debug_info) {
39+
llvm::sys::swapByteOrder(debug_info.Uuid.Data1);
40+
llvm::sys::swapByteOrder(debug_info.Uuid.Data2);
41+
llvm::sys::swapByteOrder(debug_info.Uuid.Data3);
42+
llvm::sys::swapByteOrder(debug_info.Age);
43+
if (debug_info.Age)
44+
return UUID::fromOptionalData(&debug_info, sizeof(debug_info));
45+
return UUID::fromOptionalData(&debug_info.Uuid, sizeof(debug_info.Uuid));
46+
}
47+
3848
std::string UUID::GetAsString(llvm::StringRef separator) const {
3949
std::string result;
4050
llvm::raw_string_ostream os(result);

0 commit comments

Comments
 (0)