| 1 |
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 |
/* vim:set ts=2 sw=2 sts=2 et cindent: */ |
| 3 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#ifndef SHARED_LIBRARIES_H_ |
| 8 |
#define SHARED_LIBRARIES_H_ |
| 9 |
|
| 10 |
#include "BaseProfiler.h" |
| 11 |
|
| 12 |
#include <algorithm> |
| 13 |
#include <stdint.h> |
| 14 |
#include <stdlib.h> |
| 15 |
#include <string> |
| 16 |
#include <vector> |
| 17 |
#include <tuple> |
| 18 |
|
| 19 |
namespace IPC { |
| 20 |
template <typename T> |
| 21 |
struct ParamTraits; |
| 22 |
} // namespace IPC |
| 23 |
|
| 24 |
class SharedLibrary { |
| 25 |
public: |
| 26 |
SharedLibrary(uintptr_t aStart, uintptr_t aEnd, uintptr_t aOffset, |
| 27 |
const std::string& aBreakpadId, const std::string& aCodeId, |
| 28 |
const std::string& aModuleName, const std::string& aModulePath, |
| 29 |
const std::string& aDebugName, const std::string& aDebugPath, |
| 30 |
const std::string& aVersion, const char* aArch) |
| 31 |
: mStart(aStart), |
| 32 |
mEnd(aEnd), |
| 33 |
mOffset(aOffset), |
| 34 |
mBreakpadId(aBreakpadId), |
| 35 |
mCodeId(aCodeId), |
| 36 |
mModuleName(aModuleName), |
| 37 |
mModulePath(aModulePath), |
| 38 |
mDebugName(aDebugName), |
| 39 |
mDebugPath(aDebugPath), |
| 40 |
mVersion(aVersion), |
| 41 |
mArch(aArch) {} |
| 42 |
|
| 43 |
bool operator==(const SharedLibrary& other) const { |
| 44 |
return (mStart == other.mStart) && (mEnd == other.mEnd) && |
| 45 |
(mOffset == other.mOffset) && (mModuleName == other.mModuleName) && |
| 46 |
(mModulePath == other.mModulePath) && |
| 47 |
(mDebugName == other.mDebugName) && |
| 48 |
(mDebugPath == other.mDebugPath) && |
| 49 |
(mBreakpadId == other.mBreakpadId) && (mCodeId == other.mCodeId) && |
| 50 |
(mVersion == other.mVersion) && (mArch == other.mArch); |
| 51 |
} |
| 52 |
|
| 53 |
uintptr_t GetStart() const { return mStart; } |
| 54 |
uintptr_t GetEnd() const { return mEnd; } |
| 55 |
uintptr_t GetOffset() const { return mOffset; } |
| 56 |
const std::string& GetBreakpadId() const { return mBreakpadId; } |
| 57 |
const std::string& GetCodeId() const { return mCodeId; } |
| 58 |
const std::string& GetModuleName() const { return mModuleName; } |
| 59 |
const std::string& GetModulePath() const { return mModulePath; } |
| 60 |
const std::string& GetDebugName() const { return mDebugName; } |
| 61 |
const std::string& GetDebugPath() const { return mDebugPath; } |
| 62 |
const std::string& GetVersion() const { return mVersion; } |
| 63 |
const std::string& GetArch() const { return mArch; } |
| 64 |
size_t SizeOf() const { |
| 65 |
return sizeof *this + mBreakpadId.length() + mCodeId.length() + |
| 66 |
mModuleName.length() + mModulePath.length() + mDebugName.length() + |
| 67 |
mDebugPath.length() + mVersion.length() + mArch.size(); |
| 68 |
} |
| 69 |
|
| 70 |
SharedLibrary() : mStart{0}, mEnd{0}, mOffset{0} {} |
| 71 |
|
| 72 |
private: |
| 73 |
uintptr_t mStart; |
| 74 |
uintptr_t mEnd; |
| 75 |
uintptr_t mOffset; |
| 76 |
std::string mBreakpadId; |
| 77 |
// A string carrying an identifier for a binary. |
| 78 |
// |
| 79 |
// All platforms have different formats: |
| 80 |
// - Windows: The code ID for a Windows PE file. |
| 81 |
// It's the PE timestamp and PE image size. |
| 82 |
// - macOS: The code ID for a macOS / iOS binary (mach-O). |
| 83 |
// It's the mach-O UUID without dashes and without the trailing 0 for the |
| 84 |
// breakpad ID. |
| 85 |
// - Linux/Android: The code ID for a Linux ELF file. |
| 86 |
// It's the complete build ID, as hex string. |
| 87 |
std::string mCodeId; |
| 88 |
std::string mModuleName; |
| 89 |
std::string mModulePath; |
| 90 |
std::string mDebugName; |
| 91 |
std::string mDebugPath; |
| 92 |
std::string mVersion; |
| 93 |
std::string mArch; |
| 94 |
|
| 95 |
friend struct IPC::ParamTraits<SharedLibrary>; |
| 96 |
}; |
| 97 |
|
| 98 |
static bool CompareAddresses(const SharedLibrary& first, |
| 99 |
const SharedLibrary& second) { |
| 100 |
return first.GetStart() < second.GetStart(); |
| 101 |
} |
| 102 |
|
| 103 |
class SharedLibraryInfo { |
| 104 |
public: |
| 105 |
#ifdef MOZ_GECKO_PROFILER |
| 106 |
MFBT_API static SharedLibraryInfo GetInfoForSelf(); |
| 107 |
# ifdef XP_WIN |
| 108 |
MFBT_API static SharedLibraryInfo GetInfoFromPath(const wchar_t* aPath); |
| 109 |
# endif |
| 110 |
|
| 111 |
MFBT_API static void Initialize(); |
| 112 |
#else |
| 113 |
static SharedLibraryInfo GetInfoForSelf() { return SharedLibraryInfo(); } |
| 114 |
# ifdef XP_WIN |
| 115 |
static SharedLibraryInfo GetInfoFromPath(const wchar_t* aPath) { |
| 116 |
return SharedLibraryInfo(); |
| 117 |
} |
| 118 |
# endif |
| 119 |
|
| 120 |
static void Initialize() {} |
| 121 |
#endif |
| 122 |
|
| 123 |
void AddSharedLibrary(SharedLibrary entry) { mEntries.push_back(entry); } |
| 124 |
|
| 125 |
void AddAllSharedLibraries(const SharedLibraryInfo& sharedLibraryInfo) { |
| 126 |
mEntries.insert(mEntries.end(), sharedLibraryInfo.mEntries.begin(), |
| 127 |
sharedLibraryInfo.mEntries.end()); |
| 128 |
} |
| 129 |
|
| 130 |
const SharedLibrary& GetEntry(size_t i) const { return mEntries[i]; } |
| 131 |
|
| 132 |
SharedLibrary& GetMutableEntry(size_t i) { return mEntries[i]; } |
| 133 |
|
| 134 |
// Removes items in the range [first, last) |
| 135 |
// i.e. element at the "last" index is not removed |
| 136 |
void RemoveEntries(size_t first, size_t last) { |
| 137 |
mEntries.erase(mEntries.begin() + first, mEntries.begin() + last); |
| 138 |
} |
| 139 |
|
| 140 |
bool Contains(const SharedLibrary& searchItem) const { |
| 141 |
return (mEntries.end() != |
| 142 |
std::find(mEntries.begin(), mEntries.end(), searchItem)); |
| 143 |
} |
| 144 |
|
| 145 |
size_t GetSize() const { return mEntries.size(); } |
| 146 |
|
| 147 |
void SortByAddress() { |
| 148 |
std::sort(mEntries.begin(), mEntries.end(), CompareAddresses); |
| 149 |
} |
| 150 |
|
| 151 |
// Remove duplicate entries from the vector. |
| 152 |
// |
| 153 |
// We purposefully don't use the operator== implementation of SharedLibrary |
| 154 |
// because it compares all the fields including mStart, mEnd and mOffset which |
| 155 |
// are not the same across different processes. |
| 156 |
void DeduplicateEntries() { |
| 157 |
static auto cmpSort = [](const SharedLibrary& a, const SharedLibrary& b) { |
| 158 |
return std::tie(a.GetModuleName(), a.GetBreakpadId()) < |
| 159 |
std::tie(b.GetModuleName(), b.GetBreakpadId()); |
| 160 |
}; |
| 161 |
static auto cmpEqual = [](const SharedLibrary& a, const SharedLibrary& b) { |
| 162 |
return std::tie(a.GetModuleName(), a.GetBreakpadId()) == |
| 163 |
std::tie(b.GetModuleName(), b.GetBreakpadId()); |
| 164 |
}; |
| 165 |
// std::unique requires the vector to be sorted first. It can only remove |
| 166 |
// consecutive duplicate elements. |
| 167 |
std::sort(mEntries.begin(), mEntries.end(), cmpSort); |
| 168 |
// Remove the duplicates since it's sorted now. |
| 169 |
mEntries.erase(std::unique(mEntries.begin(), mEntries.end(), cmpEqual), |
| 170 |
mEntries.end()); |
| 171 |
} |
| 172 |
|
| 173 |
void Clear() { mEntries.clear(); } |
| 174 |
|
| 175 |
size_t SizeOf() const { |
| 176 |
size_t size = 0; |
| 177 |
|
| 178 |
for (const auto& item : mEntries) { |
| 179 |
size += item.SizeOf(); |
| 180 |
} |
| 181 |
|
| 182 |
return size; |
| 183 |
} |
| 184 |
|
| 185 |
private: |
| 186 |
std::vector<SharedLibrary> mEntries; |
| 187 |
|
| 188 |
friend struct IPC::ParamTraits<SharedLibraryInfo>; |
| 189 |
}; |
| 190 |
|
| 191 |
#endif // SHARED_LIBRARIES_H_ |