From 25d320785f4242d8003e7c3a44307bb9e77221e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Naz=C4=B1m=20Can=20Alt=C4=B1nova?= Date: Tue, 14 Feb 2023 14:50:57 +0000 Subject: [PATCH] Bug 1811068 - Implement the codeId for Windows r=mstange Differential Revision: https://phabricator.services.mozilla.com/D169519 --- .../core/shared-libraries-win32.cc | 29 ++++++++++++++----- mozglue/misc/NativeNt.h | 9 ++++++ tools/profiler/core/shared-libraries-win32.cc | 20 +++++++++---- 3 files changed, 46 insertions(+), 12 deletions(-) diff --git a/mozglue/baseprofiler/core/shared-libraries-win32.cc b/mozglue/baseprofiler/core/shared-libraries-win32.cc index 4abafa8feadb..3131feda20d1 100644 --- a/mozglue/baseprofiler/core/shared-libraries-win32.cc +++ b/mozglue/baseprofiler/core/shared-libraries-win32.cc @@ -15,22 +15,29 @@ #include #include -static constexpr char digits[16] = {'0', '1', '2', '3', '4', '5', '6', '7', - '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; +static constexpr char uppercaseDigits[16] = {'0', '1', '2', '3', '4', '5', + '6', '7', '8', '9', 'A', 'B', + 'C', 'D', 'E', 'F'}; +static constexpr char lowercaseDigits[16] = {'0', '1', '2', '3', '4', '5', + '6', '7', '8', '9', 'a', 'b', + 'c', 'd', 'e', 'f'}; static void AppendHex(const unsigned char* aBegin, const unsigned char* aEnd, std::string& aOut) { for (const unsigned char* p = aBegin; p < aEnd; ++p) { unsigned char c = *p; - aOut += digits[c >> 4]; - aOut += digits[c & 0xFu]; + aOut += uppercaseDigits[c >> 4]; + aOut += uppercaseDigits[c & 0xFu]; } } static constexpr bool WITH_PADDING = true; static constexpr bool WITHOUT_PADDING = false; +static constexpr bool LOWERCASE = true; +static constexpr bool UPPERCASE = false; template -static void AppendHex(T aValue, std::string& aOut, bool aWithPadding) { +static void AppendHex(T aValue, std::string& aOut, bool aWithPadding, + bool aLowercase = UPPERCASE) { for (int i = sizeof(T) * 2 - 1; i >= 0; --i) { unsigned nibble = (aValue >> (i * 4)) & 0xFu; // If no-padding requested, skip starting zeroes -- unless we're on the very @@ -44,7 +51,7 @@ static void AppendHex(T aValue, std::string& aOut, bool aWithPadding) { // so we don't skip zeroes anymore. aWithPadding = true; } - aOut += digits[nibble]; + aOut += aLowercase ? lowercaseDigits[nibble] : uppercaseDigits[nibble]; } } @@ -167,6 +174,14 @@ SharedLibraryInfo SharedLibraryInfo::GetInfoForSelf() { (pos != std::string::npos) ? pdbPathStr.substr(pos + 1) : pdbPathStr; } + std::string codeId; + DWORD timestamp; + DWORD imageSize; + if (headers.GetTimeStamp(timestamp) && headers.GetImageSize(imageSize)) { + AppendHex(timestamp, codeId, WITH_PADDING); + AppendHex(imageSize, codeId, WITHOUT_PADDING, LOWERCASE); + } + std::string versionStr; uint64_t version; if (headers.GetVersionInfo(version)) { @@ -181,7 +196,7 @@ SharedLibraryInfo SharedLibraryInfo::GetInfoForSelf() { SharedLibrary shlib(modStart, modEnd, 0, // DLLs are always mapped at offset 0 on Windows - breakpadId, std::string{}, moduleNameStr, modulePathStr, + breakpadId, codeId, moduleNameStr, modulePathStr, pdbNameStr, pdbPathStr, versionStr, ""); sharedLibraryInfo.AddSharedLibrary(shlib); }; diff --git a/mozglue/misc/NativeNt.h b/mozglue/misc/NativeNt.h index 1cce66731711..d172259e5376 100644 --- a/mozglue/misc/NativeNt.h +++ b/mozglue/misc/NativeNt.h @@ -726,6 +726,15 @@ class MOZ_RAII PEHeaders final { return true; } + bool GetImageSize(DWORD& aResult) const { + if (!(*this)) { + return false; + } + + aResult = mPeHeader->OptionalHeader.SizeOfImage; + return true; + } + PIMAGE_IMPORT_DESCRIPTOR GetImportDescriptor(const char* aModuleNameASCII) const { for (PIMAGE_IMPORT_DESCRIPTOR curImpDesc = GetImportDirectory(); diff --git a/tools/profiler/core/shared-libraries-win32.cc b/tools/profiler/core/shared-libraries-win32.cc index 11ca6c21e27a..0fc46cd38664 100644 --- a/tools/profiler/core/shared-libraries-win32.cc +++ b/tools/profiler/core/shared-libraries-win32.cc @@ -115,6 +115,16 @@ SharedLibraryInfo SharedLibraryInfo::GetInfoForSelf() { pdbPathStr = NS_ConvertUTF8toUTF16(debugInfo->pdbFileName); } + nsAutoCString codeId; + DWORD timestamp; + DWORD imageSize; + if (headers.GetTimeStamp(timestamp) && headers.GetImageSize(imageSize)) { + codeId.AppendPrintf( + "%08lX" // Uppercase 8 digits of hex timestamp with leading zeroes. + "%lx", // Lowercase hex image size + timestamp, imageSize); + } + nsAutoCString versionStr; uint64_t version; if (headers.GetVersionInfo(version)) { @@ -127,11 +137,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, nsCString(), PromiseFlatString(moduleNameStr), - nsDependentString(aModulePath), pdbNameStr, pdbPathStr, versionStr, ""); + SharedLibrary shlib(modStart, modEnd, + 0, // DLLs are always mapped at offset 0 on Windows + breakpadId, codeId, PromiseFlatString(moduleNameStr), + nsDependentString(aModulePath), pdbNameStr, pdbPathStr, + versionStr, ""); sharedLibraryInfo.AddSharedLibrary(shlib); };