2011-12-16 19:56:06 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-12-16 19:56:06 +04:00
|
|
|
|
|
|
|
#include <windows.h>
|
2012-03-15 16:44:02 +04:00
|
|
|
#include <dbghelp.h>
|
2013-01-10 00:05:00 +04:00
|
|
|
#include <sstream>
|
2017-02-18 17:17:30 +03:00
|
|
|
#include <psapi.h>
|
2011-12-16 19:56:06 +04:00
|
|
|
|
|
|
|
#include "shared-libraries.h"
|
2012-01-27 04:03:42 +04:00
|
|
|
#include "nsWindowsHelpers.h"
|
2017-02-18 17:17:30 +03:00
|
|
|
#include "mozilla/UniquePtr.h"
|
|
|
|
#include "mozilla/Unused.h"
|
|
|
|
#include "nsNativeCharsetUtils.h"
|
2011-12-16 19:56:06 +04:00
|
|
|
|
2012-03-12 15:07:05 +04:00
|
|
|
#define CV_SIGNATURE 0x53445352 // 'SDSR'
|
|
|
|
|
|
|
|
struct CodeViewRecord70
|
|
|
|
{
|
|
|
|
uint32_t signature;
|
|
|
|
GUID pdbSignature;
|
|
|
|
uint32_t pdbAge;
|
2017-02-18 17:17:30 +03:00
|
|
|
// A UTF-8 string, according to https://github.com/Microsoft/microsoft-pdb/blob/082c5290e5aff028ae84e43affa8be717aa7af73/PDB/dbi/locator.cpp#L785
|
2012-03-20 03:05:25 +04:00
|
|
|
char pdbFileName[1];
|
2012-03-12 15:07:05 +04:00
|
|
|
};
|
|
|
|
|
2012-03-20 03:05:25 +04:00
|
|
|
static bool GetPdbInfo(uintptr_t aStart, nsID& aSignature, uint32_t& aAge, char** aPdbName)
|
2012-03-12 15:07:05 +04:00
|
|
|
{
|
|
|
|
if (!aStart) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
PIMAGE_DOS_HEADER dosHeader = reinterpret_cast<PIMAGE_DOS_HEADER>(aStart);
|
|
|
|
if (dosHeader->e_magic != IMAGE_DOS_SIGNATURE) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
PIMAGE_NT_HEADERS ntHeaders = reinterpret_cast<PIMAGE_NT_HEADERS>(
|
|
|
|
aStart + dosHeader->e_lfanew);
|
|
|
|
if (ntHeaders->Signature != IMAGE_NT_SIGNATURE) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t relativeVirtualAddress =
|
|
|
|
ntHeaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress;
|
|
|
|
if (!relativeVirtualAddress) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
PIMAGE_DEBUG_DIRECTORY debugDirectory =
|
|
|
|
reinterpret_cast<PIMAGE_DEBUG_DIRECTORY>(aStart + relativeVirtualAddress);
|
|
|
|
if (!debugDirectory || debugDirectory->Type != IMAGE_DEBUG_TYPE_CODEVIEW) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CodeViewRecord70 *debugInfo = reinterpret_cast<CodeViewRecord70 *>(
|
|
|
|
aStart + debugDirectory->AddressOfRawData);
|
|
|
|
if (!debugInfo || debugInfo->signature != CV_SIGNATURE) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
aAge = debugInfo->pdbAge;
|
|
|
|
GUID& pdbSignature = debugInfo->pdbSignature;
|
|
|
|
aSignature.m0 = pdbSignature.Data1;
|
|
|
|
aSignature.m1 = pdbSignature.Data2;
|
|
|
|
aSignature.m2 = pdbSignature.Data3;
|
|
|
|
memcpy(aSignature.m3, pdbSignature.Data4, sizeof(pdbSignature.Data4));
|
2012-03-20 03:05:25 +04:00
|
|
|
|
|
|
|
// 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
|
2017-02-18 17:17:30 +03:00
|
|
|
*aPdbName = debugInfo->pdbFileName;
|
2012-03-20 03:05:25 +04:00
|
|
|
|
2012-03-12 15:07:05 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-01-10 00:05:00 +04:00
|
|
|
static bool IsDashOrBraces(char c)
|
|
|
|
{
|
|
|
|
return c == '-' || c == '{' || c == '}';
|
|
|
|
}
|
|
|
|
|
2017-02-18 17:17:30 +03:00
|
|
|
std::string GetVersion(WCHAR* dllPath)
|
|
|
|
{
|
|
|
|
DWORD infoSize = GetFileVersionInfoSizeW(dllPath, nullptr);
|
|
|
|
if (infoSize == 0) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
mozilla::UniquePtr<unsigned char[]> infoData = mozilla::MakeUnique<unsigned char[]>(infoSize);
|
|
|
|
if (!GetFileVersionInfoW(dllPath, 0, infoSize, infoData.get())) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
VS_FIXEDFILEINFO* vInfo;
|
|
|
|
UINT vInfoLen;
|
|
|
|
if (!VerQueryValueW(infoData.get(), L"\\", (LPVOID*)&vInfo, &vInfoLen)) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
if (!vInfo) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ostringstream stream;
|
|
|
|
stream << (vInfo->dwFileVersionMS >> 16) << "."
|
|
|
|
<< (vInfo->dwFileVersionMS & 0xFFFF) << "."
|
|
|
|
<< (vInfo->dwFileVersionLS >> 16) << "."
|
|
|
|
<< (vInfo->dwFileVersionLS & 0xFFFF);
|
|
|
|
|
|
|
|
return stream.str();
|
|
|
|
}
|
|
|
|
|
2011-12-16 19:56:06 +04:00
|
|
|
SharedLibraryInfo SharedLibraryInfo::GetInfoForSelf()
|
|
|
|
{
|
|
|
|
SharedLibraryInfo sharedLibraryInfo;
|
|
|
|
|
2017-02-18 17:17:30 +03:00
|
|
|
HANDLE hProcess = GetCurrentProcess();
|
|
|
|
mozilla::UniquePtr<HMODULE[]> hMods;
|
|
|
|
size_t modulesNum = 0;
|
|
|
|
if (hProcess != NULL) {
|
|
|
|
DWORD modulesSize;
|
|
|
|
if (!EnumProcessModules(hProcess, nullptr, 0, &modulesSize)) {
|
|
|
|
return sharedLibraryInfo;
|
|
|
|
}
|
|
|
|
modulesNum = modulesSize / sizeof(HMODULE);
|
|
|
|
hMods = mozilla::MakeUnique<HMODULE[]>(modulesNum);
|
|
|
|
if (!EnumProcessModules(hProcess, hMods.get(), modulesNum * sizeof(HMODULE), &modulesSize)) {
|
|
|
|
return sharedLibraryInfo;
|
|
|
|
}
|
2017-03-06 04:10:36 +03:00
|
|
|
// The list may have shrunk between calls
|
|
|
|
if (modulesSize / sizeof(HMODULE) < modulesNum) {
|
|
|
|
modulesNum = modulesSize / sizeof(HMODULE);
|
|
|
|
}
|
2017-02-18 17:17:30 +03:00
|
|
|
}
|
|
|
|
|
2017-02-24 21:03:48 +03:00
|
|
|
for (unsigned int i = 0; i < modulesNum; i++) {
|
2017-02-18 17:17:30 +03:00
|
|
|
nsID pdbSig;
|
|
|
|
uint32_t pdbAge;
|
2017-03-15 01:26:50 +03:00
|
|
|
nsAutoString pdbPathStr;
|
2017-02-18 17:17:30 +03:00
|
|
|
nsAutoString pdbNameStr;
|
|
|
|
char *pdbName = NULL;
|
|
|
|
std::string breakpadId;
|
|
|
|
WCHAR modulePath[MAX_PATH + 1];
|
|
|
|
|
2017-02-24 21:03:48 +03:00
|
|
|
if (!GetModuleFileNameEx(hProcess, hMods[i], modulePath, sizeof(modulePath) / sizeof(WCHAR))) {
|
2017-02-18 17:17:30 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
MODULEINFO module = {0};
|
2017-02-24 21:03:48 +03:00
|
|
|
if (!GetModuleInformation(hProcess, hMods[i], &module, sizeof(MODULEINFO))) {
|
2017-02-18 17:17:30 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-03-06 04:10:36 +03:00
|
|
|
// Load the module again to make sure that its handle will remain
|
2017-02-18 17:17:30 +03:00
|
|
|
// valid as we attempt to read the PDB information from it. We load the
|
|
|
|
// DLL as a datafile so that if the module actually gets unloaded between
|
|
|
|
// the call to EnumProcessModules and the following LoadLibraryEx, we don't
|
|
|
|
// end up running the now newly loaded module's DllMain function. If the
|
|
|
|
// module is already loaded, LoadLibraryEx just increments its refcount.
|
|
|
|
//
|
|
|
|
// Note that because of the race condition above, merely loading the DLL
|
|
|
|
// again is not safe enough, therefore we also need to make sure that we
|
|
|
|
// can read the memory mapped at the base address before we can safely
|
|
|
|
// proceed to actually access those pages.
|
|
|
|
HMODULE handleLock = LoadLibraryEx(modulePath, NULL, LOAD_LIBRARY_AS_DATAFILE);
|
|
|
|
MEMORY_BASIC_INFORMATION vmemInfo = { 0 };
|
|
|
|
if (handleLock &&
|
|
|
|
sizeof(vmemInfo) == VirtualQuery(module.lpBaseOfDll, &vmemInfo, sizeof(vmemInfo)) &&
|
|
|
|
vmemInfo.State == MEM_COMMIT &&
|
|
|
|
GetPdbInfo((uintptr_t)module.lpBaseOfDll, pdbSig, pdbAge, &pdbName)) {
|
|
|
|
std::ostringstream stream;
|
|
|
|
stream << pdbSig.ToString() << std::hex << pdbAge;
|
|
|
|
breakpadId = stream.str();
|
|
|
|
std::string::iterator end =
|
|
|
|
std::remove_if(breakpadId.begin(), breakpadId.end(), IsDashOrBraces);
|
|
|
|
breakpadId.erase(end, breakpadId.end());
|
|
|
|
std::transform(breakpadId.begin(), breakpadId.end(),
|
|
|
|
breakpadId.begin(), toupper);
|
|
|
|
|
2017-03-15 01:26:50 +03:00
|
|
|
pdbPathStr = NS_ConvertUTF8toUTF16(pdbName);
|
|
|
|
pdbNameStr = pdbPathStr;
|
2017-02-18 17:17:30 +03:00
|
|
|
int32_t pos = pdbNameStr.RFindChar('\\');
|
|
|
|
if (pos != kNotFound) {
|
|
|
|
pdbNameStr.Cut(0, pos + 1);
|
2012-03-12 15:07:05 +04:00
|
|
|
}
|
2017-02-18 17:17:30 +03:00
|
|
|
}
|
|
|
|
|
2017-03-15 01:26:50 +03:00
|
|
|
nsAutoString modulePathStr(modulePath);
|
|
|
|
nsAutoString moduleNameStr = modulePathStr;
|
2017-03-15 00:07:43 +03:00
|
|
|
int32_t pos = moduleNameStr.RFindChar('\\');
|
2017-02-18 17:17:30 +03:00
|
|
|
if (pos != kNotFound) {
|
2017-03-15 00:07:43 +03:00
|
|
|
moduleNameStr.Cut(0, pos + 1);
|
2017-02-18 17:17:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
SharedLibrary shlib((uintptr_t)module.lpBaseOfDll,
|
|
|
|
(uintptr_t)module.lpBaseOfDll + module.SizeOfImage,
|
|
|
|
0, // DLLs are always mapped at offset 0 on Windows
|
|
|
|
breakpadId,
|
2017-03-15 00:07:43 +03:00
|
|
|
moduleNameStr,
|
2017-03-15 01:26:50 +03:00
|
|
|
modulePathStr,
|
2017-02-18 17:17:30 +03:00
|
|
|
pdbNameStr,
|
2017-03-15 01:26:50 +03:00
|
|
|
pdbPathStr,
|
2017-03-14 00:51:41 +03:00
|
|
|
GetVersion(modulePath),
|
|
|
|
"");
|
2017-02-18 17:17:30 +03:00
|
|
|
sharedLibraryInfo.AddSharedLibrary(shlib);
|
|
|
|
|
|
|
|
FreeLibrary(handleLock); // ok to free null handles
|
2012-01-27 04:03:42 +04:00
|
|
|
}
|
2011-12-16 19:56:06 +04:00
|
|
|
|
|
|
|
return sharedLibraryInfo;
|
|
|
|
}
|
|
|
|
|
2017-07-10 22:29:35 +03:00
|
|
|
void
|
|
|
|
SharedLibraryInfo::Initialize()
|
|
|
|
{
|
|
|
|
/* do nothing */
|
|
|
|
}
|