Skip to content

Commit

Permalink
Bug 1811068 - Add codeId field to SharedLibrary class and output it i…
Browse files Browse the repository at this point in the history
…n profile json r=mstange

Differential Revision: https://phabricator.services.mozilla.com/D169515
  • Loading branch information
canova committed Feb 14, 2023
1 parent 5cdd096 commit ef8e434
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 118 deletions.
1 change: 1 addition & 0 deletions mozglue/baseprofiler/core/platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1768,6 +1768,7 @@ static void AddSharedLibraryInfoToStream(JSONWriter& aWriter,
aWriter.StringProperty("debugName", aLib.GetDebugName());
aWriter.StringProperty("debugPath", aLib.GetDebugPath());
aWriter.StringProperty("breakpadId", aLib.GetBreakpadId());
aWriter.StringProperty("codeId", aLib.GetCodeId());
aWriter.StringProperty("arch", aLib.GetArch());
aWriter.EndObject();
}
Expand Down
4 changes: 2 additions & 2 deletions mozglue/baseprofiler/core/shared-libraries-linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -678,8 +678,8 @@ static SharedLibrary SharedLibraryAtPath(const char* path,
std::string nameStr =
(pos != std::string::npos) ? pathStr.substr(pos + 1) : pathStr;

return SharedLibrary(libStart, libEnd, offset, getId(path), nameStr, pathStr,
nameStr, pathStr, std::string{}, "");
return SharedLibrary(libStart, libEnd, offset, getId(path), std::string{},
nameStr, pathStr, nameStr, pathStr, std::string{}, "");
}

static int dl_iterate_callback(struct dl_phdr_info* dl_info, size_t size,
Expand Down
6 changes: 3 additions & 3 deletions mozglue/baseprofiler/core/shared-libraries-macos.cc
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,9 @@ static void addSharedLibrary(const platform_mach_header* header,
const NXArchInfo* archInfo =
NXGetArchInfoFromCpuType(header->cputype, header->cpusubtype);

info.AddSharedLibrary(SharedLibrary(start, start + size, 0, uuid, nameStr,
pathStr, nameStr, pathStr, std::string{},
archInfo ? archInfo->name : ""));
info.AddSharedLibrary(SharedLibrary(
start, start + size, 0, uuid, std::string{}, nameStr, pathStr, nameStr,
pathStr, std::string{}, archInfo ? archInfo->name : ""));
}

// Translate the statically stored sSharedLibrariesList information into a
Expand Down
184 changes: 92 additions & 92 deletions mozglue/baseprofiler/core/shared-libraries-win32.cc
Original file line number Diff line number Diff line change
Expand Up @@ -93,98 +93,98 @@ static bool IsModuleUnsafeToLoad(const std::string& aModuleName) {
SharedLibraryInfo SharedLibraryInfo::GetInfoForSelf() {
SharedLibraryInfo sharedLibraryInfo;

auto addSharedLibraryFromModuleInfo =
[&sharedLibraryInfo](const wchar_t* aModulePath, HMODULE aModule) {
mozilla::UniquePtr<char[]> utf8ModulePath(
mozilla::glue::WideToUTF8(aModulePath));
if (!utf8ModulePath) {
return;
}

std::string modulePathStr(utf8ModulePath.get());
size_t pos = modulePathStr.find_last_of("\\/");
std::string moduleNameStr = (pos != std::string::npos)
? modulePathStr.substr(pos + 1)
: modulePathStr;

// If the module is unsafe to call LoadLibraryEx for, we skip.
if (IsModuleUnsafeToLoad(moduleNameStr)) {
return;
}

// Load the module again to make sure that its handle will remain
// valid as we attempt to read the PDB information from it. We load the
// DLL as a datafile so that we don't end up running the newly loaded
// module's DllMain function. If the original handle |aModule| is
// valid, LoadLibraryEx just increments its refcount.
// LOAD_LIBRARY_AS_IMAGE_RESOURCE is needed to read information from the
// sections (not PE headers) which should be relocated by the loader,
// otherwise GetPdbInfo() will cause a crash.
nsModuleHandle handleLock(::LoadLibraryExW(
aModulePath, NULL,
LOAD_LIBRARY_AS_DATAFILE | LOAD_LIBRARY_AS_IMAGE_RESOURCE));
if (!handleLock) {
return;
}

mozilla::nt::PEHeaders headers(handleLock.get());
if (!headers) {
return;
}

mozilla::Maybe<mozilla::Range<const uint8_t>> bounds =
headers.GetBounds();
if (!bounds) {
return;
}

// Put the original |aModule| into SharedLibrary, but we get debug info
// from |handleLock| as |aModule| might be inaccessible.
const uintptr_t modStart = reinterpret_cast<uintptr_t>(aModule);
const uintptr_t modEnd = modStart + bounds->length();

std::string breakpadId;
std::string pdbPathStr;
std::string pdbNameStr;
if (const auto* debugInfo = headers.GetPdbInfo()) {
MOZ_ASSERT(breakpadId.empty());
const GUID& pdbSig = debugInfo->pdbSignature;
AppendHex(pdbSig.Data1, breakpadId, WITH_PADDING);
AppendHex(pdbSig.Data2, breakpadId, WITH_PADDING);
AppendHex(pdbSig.Data3, breakpadId, WITH_PADDING);
AppendHex(reinterpret_cast<const unsigned char*>(&pdbSig.Data4),
reinterpret_cast<const unsigned char*>(&pdbSig.Data4) +
sizeof(pdbSig.Data4),
breakpadId);
AppendHex(debugInfo->pdbAge, breakpadId, WITHOUT_PADDING);

// The PDB file name could be different from module filename,
// so report both
// e.g. The PDB for C:\Windows\SysWOW64\ntdll.dll is wntdll.pdb
pdbPathStr = debugInfo->pdbFileName;
size_t pos = pdbPathStr.find_last_of("\\/");
pdbNameStr = (pos != std::string::npos) ? pdbPathStr.substr(pos + 1)
: pdbPathStr;
}

std::string versionStr;
uint64_t version;
if (headers.GetVersionInfo(version)) {
versionStr += std::to_string((version >> 48) & 0xFFFF);
versionStr += '.';
versionStr += std::to_string((version >> 32) & 0xFFFF);
versionStr += '.';
versionStr += std::to_string((version >> 16) & 0xFFFF);
versionStr += '.';
versionStr += std::to_string(version & 0xFFFF);
}

SharedLibrary shlib(modStart, modEnd,
0, // DLLs are always mapped at offset 0 on Windows
breakpadId, moduleNameStr, modulePathStr,
pdbNameStr, pdbPathStr, versionStr, "");
sharedLibraryInfo.AddSharedLibrary(shlib);
};
auto addSharedLibraryFromModuleInfo = [&sharedLibraryInfo](
const wchar_t* aModulePath,
HMODULE aModule) {
mozilla::UniquePtr<char[]> utf8ModulePath(
mozilla::glue::WideToUTF8(aModulePath));
if (!utf8ModulePath) {
return;
}

std::string modulePathStr(utf8ModulePath.get());
size_t pos = modulePathStr.find_last_of("\\/");
std::string moduleNameStr = (pos != std::string::npos)
? modulePathStr.substr(pos + 1)
: modulePathStr;

// If the module is unsafe to call LoadLibraryEx for, we skip.
if (IsModuleUnsafeToLoad(moduleNameStr)) {
return;
}

// Load the module again to make sure that its handle will remain
// valid as we attempt to read the PDB information from it. We load the
// DLL as a datafile so that we don't end up running the newly loaded
// module's DllMain function. If the original handle |aModule| is
// valid, LoadLibraryEx just increments its refcount.
// LOAD_LIBRARY_AS_IMAGE_RESOURCE is needed to read information from the
// sections (not PE headers) which should be relocated by the loader,
// otherwise GetPdbInfo() will cause a crash.
nsModuleHandle handleLock(::LoadLibraryExW(
aModulePath, NULL,
LOAD_LIBRARY_AS_DATAFILE | LOAD_LIBRARY_AS_IMAGE_RESOURCE));
if (!handleLock) {
return;
}

mozilla::nt::PEHeaders headers(handleLock.get());
if (!headers) {
return;
}

mozilla::Maybe<mozilla::Range<const uint8_t>> bounds = headers.GetBounds();
if (!bounds) {
return;
}

// Put the original |aModule| into SharedLibrary, but we get debug info
// from |handleLock| as |aModule| might be inaccessible.
const uintptr_t modStart = reinterpret_cast<uintptr_t>(aModule);
const uintptr_t modEnd = modStart + bounds->length();

std::string breakpadId;
std::string pdbPathStr;
std::string pdbNameStr;
if (const auto* debugInfo = headers.GetPdbInfo()) {
MOZ_ASSERT(breakpadId.empty());
const GUID& pdbSig = debugInfo->pdbSignature;
AppendHex(pdbSig.Data1, breakpadId, WITH_PADDING);
AppendHex(pdbSig.Data2, breakpadId, WITH_PADDING);
AppendHex(pdbSig.Data3, breakpadId, WITH_PADDING);
AppendHex(reinterpret_cast<const unsigned char*>(&pdbSig.Data4),
reinterpret_cast<const unsigned char*>(&pdbSig.Data4) +
sizeof(pdbSig.Data4),
breakpadId);
AppendHex(debugInfo->pdbAge, breakpadId, WITHOUT_PADDING);

// The PDB file name could be different from module filename,
// so report both
// e.g. The PDB for C:\Windows\SysWOW64\ntdll.dll is wntdll.pdb
pdbPathStr = debugInfo->pdbFileName;
size_t pos = pdbPathStr.find_last_of("\\/");
pdbNameStr =
(pos != std::string::npos) ? pdbPathStr.substr(pos + 1) : pdbPathStr;
}

std::string versionStr;
uint64_t version;
if (headers.GetVersionInfo(version)) {
versionStr += std::to_string((version >> 48) & 0xFFFF);
versionStr += '.';
versionStr += std::to_string((version >> 32) & 0xFFFF);
versionStr += '.';
versionStr += std::to_string((version >> 16) & 0xFFFF);
versionStr += '.';
versionStr += std::to_string(version & 0xFFFF);
}

SharedLibrary shlib(modStart, modEnd,
0, // DLLs are always mapped at offset 0 on Windows
breakpadId, std::string{}, moduleNameStr, modulePathStr,
pdbNameStr, pdbPathStr, versionStr, "");
sharedLibraryInfo.AddSharedLibrary(shlib);
};

mozilla::EnumerateProcessModules(addSharedLibraryFromModuleInfo);
return sharedLibraryInfo;
Expand Down
27 changes: 21 additions & 6 deletions mozglue/baseprofiler/public/BaseProfilerSharedLibraries.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@
class SharedLibrary {
public:
SharedLibrary(uintptr_t aStart, uintptr_t aEnd, uintptr_t aOffset,
const std::string& aBreakpadId, const std::string& aModuleName,
const std::string& aModulePath, const std::string& aDebugName,
const std::string& aDebugPath, const std::string& aVersion,
const char* aArch)
const std::string& aBreakpadId, const std::string& aCodeId,
const std::string& aModuleName, const std::string& aModulePath,
const std::string& aDebugName, const std::string& aDebugPath,
const std::string& aVersion, const char* aArch)
: mStart(aStart),
mEnd(aEnd),
mOffset(aOffset),
mBreakpadId(aBreakpadId),
mCodeId(aCodeId),
mModuleName(aModuleName),
mModulePath(aModulePath),
mDebugName(aDebugName),
Expand All @@ -42,6 +43,7 @@ class SharedLibrary {
mEnd(aEntry.mEnd),
mOffset(aEntry.mOffset),
mBreakpadId(aEntry.mBreakpadId),
mCodeId(aEntry.mCodeId),
mModuleName(aEntry.mModuleName),
mModulePath(aEntry.mModulePath),
mDebugName(aEntry.mDebugName),
Expand All @@ -57,6 +59,7 @@ class SharedLibrary {
mEnd = aEntry.mEnd;
mOffset = aEntry.mOffset;
mBreakpadId = aEntry.mBreakpadId;
mCodeId = aEntry.mCodeId;
mModuleName = aEntry.mModuleName;
mModulePath = aEntry.mModulePath;
mDebugName = aEntry.mDebugName;
Expand All @@ -72,14 +75,15 @@ class SharedLibrary {
(mModulePath == other.mModulePath) &&
(mDebugName == other.mDebugName) &&
(mDebugPath == other.mDebugPath) &&
(mBreakpadId == other.mBreakpadId) && (mVersion == other.mVersion) &&
(mArch == other.mArch);
(mBreakpadId == other.mBreakpadId) && (mCodeId == other.mCodeId) &&
(mVersion == other.mVersion) && (mArch == other.mArch);
}

uintptr_t GetStart() const { return mStart; }
uintptr_t GetEnd() const { return mEnd; }
uintptr_t GetOffset() const { return mOffset; }
const std::string& GetBreakpadId() const { return mBreakpadId; }
const std::string& GetCodeId() const { return mCodeId; }
const std::string& GetModuleName() const { return mModuleName; }
const std::string& GetModulePath() const { return mModulePath; }
const std::string& GetDebugName() const { return mDebugName; }
Expand All @@ -94,6 +98,17 @@ class SharedLibrary {
uintptr_t mEnd;
uintptr_t mOffset;
std::string mBreakpadId;
// A string carrying an identifier for a binary.
//
// All platforms have different formats:
// - Windows: The code ID for a Windows PE file.
// It's the PE timestamp and PE image size.
// - macOS: The code ID for a macOS / iOS binary (mach-O).
// It's the mach-O UUID without dashes and without the trailing 0 for the
// breakpad ID.
// - Linux/Android: The code ID for a Linux ELF file.
// It's the complete build ID, as hex string.
std::string mCodeId;
std::string mModuleName;
std::string mModulePath;
std::string mDebugName;
Expand Down
1 change: 1 addition & 0 deletions tools/profiler/core/platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2604,6 +2604,7 @@ static void AddSharedLibraryInfoToStream(JSONWriter& aWriter,
aWriter.StringProperty("debugPath",
NS_ConvertUTF16toUTF8(aLib.GetDebugPath()));
aWriter.StringProperty("breakpadId", aLib.GetBreakpadId());
aWriter.StringProperty("codeId", aLib.GetCodeId());
aWriter.StringProperty("arch", aLib.GetArch());
aWriter.EndObject();
}
Expand Down
4 changes: 2 additions & 2 deletions tools/profiler/core/shared-libraries-linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ static SharedLibrary SharedLibraryAtPath(const char* path,
nameStr.Cut(0, pos + 1);
}

return SharedLibrary(libStart, libEnd, offset, getId(path), nameStr, pathStr,
nameStr, pathStr, ""_ns, "");
return SharedLibrary(libStart, libEnd, offset, getId(path), nsCString(),
nameStr, pathStr, nameStr, pathStr, ""_ns, "");
}

static int dl_iterate_callback(struct dl_phdr_info* dl_info, size_t size,
Expand Down
4 changes: 2 additions & 2 deletions tools/profiler/core/shared-libraries-macos.cc
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ static void addSharedLibrary(const platform_mach_header* header,
const NXArchInfo* archInfo =
NXGetArchInfoFromCpuType(header->cputype, header->cpusubtype);

info.AddSharedLibrary(SharedLibrary(start, start + size, 0, uuid, nameStr,
pathStr, nameStr, pathStr, ""_ns,
info.AddSharedLibrary(SharedLibrary(start, start + size, 0, uuid, nsCString(),
nameStr, pathStr, nameStr, pathStr, ""_ns,
archInfo ? archInfo->name : ""));
}

Expand Down
10 changes: 5 additions & 5 deletions tools/profiler/core/shared-libraries-win32.cc
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ SharedLibraryInfo SharedLibraryInfo::GetInfoForSelf() {

const nsString& pdbNameStr =
PromiseFlatString(mozilla::nt::GetLeafName(pdbPathStr));
SharedLibrary shlib(modStart, modEnd,
0, // DLLs are always mapped at offset 0 on Windows
breakpadId, PromiseFlatString(moduleNameStr),
nsDependentString(aModulePath), pdbNameStr, pdbPathStr,
versionStr, "");
SharedLibrary shlib(
modStart, modEnd,
0, // DLLs are always mapped at offset 0 on Windows
breakpadId, nsCString(), PromiseFlatString(moduleNameStr),
nsDependentString(aModulePath), pdbNameStr, pdbPathStr, versionStr, "");
sharedLibraryInfo.AddSharedLibrary(shlib);
};

Expand Down
Loading

0 comments on commit ef8e434

Please sign in to comment.