14824e7fdSDimitry Andric #include <stdint.h> 24824e7fdSDimitry Andric #include <stdlib.h> 34824e7fdSDimitry Andric #include <string.h> 44824e7fdSDimitry Andric 54824e7fdSDimitry Andric #include "memprof_rawprofile.h" 64824e7fdSDimitry Andric #include "profile/MemProfData.inc" 7349cc55cSDimitry Andric #include "sanitizer_common/sanitizer_allocator_internal.h" 8*06c3fb27SDimitry Andric #include "sanitizer_common/sanitizer_array_ref.h" 94824e7fdSDimitry Andric #include "sanitizer_common/sanitizer_common.h" 10349cc55cSDimitry Andric #include "sanitizer_common/sanitizer_linux.h" 11349cc55cSDimitry Andric #include "sanitizer_common/sanitizer_procmaps.h" 12349cc55cSDimitry Andric #include "sanitizer_common/sanitizer_stackdepot.h" 13349cc55cSDimitry Andric #include "sanitizer_common/sanitizer_stackdepotbase.h" 14349cc55cSDimitry Andric #include "sanitizer_common/sanitizer_stacktrace.h" 15349cc55cSDimitry Andric #include "sanitizer_common/sanitizer_vector.h" 16349cc55cSDimitry Andric 17349cc55cSDimitry Andric namespace __memprof { 18349cc55cSDimitry Andric using ::__sanitizer::Vector; 191fd87a68SDimitry Andric using ::llvm::memprof::MemInfoBlock; 204824e7fdSDimitry Andric using SegmentEntry = ::llvm::memprof::SegmentEntry; 214824e7fdSDimitry Andric using Header = ::llvm::memprof::Header; 22349cc55cSDimitry Andric 23349cc55cSDimitry Andric namespace { 24*06c3fb27SDimitry Andric template <class T> char *WriteBytes(const T &Pod, char *Buffer) { 25349cc55cSDimitry Andric *(T *)Buffer = Pod; 26349cc55cSDimitry Andric return Buffer + sizeof(T); 27349cc55cSDimitry Andric } 28349cc55cSDimitry Andric 29349cc55cSDimitry Andric void RecordStackId(const uptr Key, UNUSED LockedMemInfoBlock *const &MIB, 30349cc55cSDimitry Andric void *Arg) { 31349cc55cSDimitry Andric // No need to touch the MIB value here since we are only recording the key. 32349cc55cSDimitry Andric auto *StackIds = reinterpret_cast<Vector<u64> *>(Arg); 33349cc55cSDimitry Andric StackIds->PushBack(Key); 34349cc55cSDimitry Andric } 35349cc55cSDimitry Andric } // namespace 36349cc55cSDimitry Andric 37*06c3fb27SDimitry Andric u64 SegmentSizeBytes(ArrayRef<LoadedModule> Modules) { 38349cc55cSDimitry Andric u64 NumSegmentsToRecord = 0; 39*06c3fb27SDimitry Andric for (const auto &Module : Modules) { 40*06c3fb27SDimitry Andric for (const auto &Segment : Module.ranges()) { 41*06c3fb27SDimitry Andric if (Segment.executable) 42349cc55cSDimitry Andric NumSegmentsToRecord++; 43*06c3fb27SDimitry Andric } 44*06c3fb27SDimitry Andric } 45349cc55cSDimitry Andric 46349cc55cSDimitry Andric return sizeof(u64) // A header which stores the number of records. 47349cc55cSDimitry Andric + sizeof(SegmentEntry) * NumSegmentsToRecord; 48349cc55cSDimitry Andric } 49349cc55cSDimitry Andric 50349cc55cSDimitry Andric // The segment section uses the following format: 51349cc55cSDimitry Andric // ---------- Segment Info 52349cc55cSDimitry Andric // Num Entries 53349cc55cSDimitry Andric // ---------- Segment Entry 54349cc55cSDimitry Andric // Start 55349cc55cSDimitry Andric // End 56349cc55cSDimitry Andric // Offset 57*06c3fb27SDimitry Andric // UuidSize 58*06c3fb27SDimitry Andric // Uuid 32B 59349cc55cSDimitry Andric // ---------- 60349cc55cSDimitry Andric // ... 61*06c3fb27SDimitry Andric void SerializeSegmentsToBuffer(ArrayRef<LoadedModule> Modules, 62349cc55cSDimitry Andric const u64 ExpectedNumBytes, char *&Buffer) { 63349cc55cSDimitry Andric char *Ptr = Buffer; 64349cc55cSDimitry Andric // Reserve space for the final count. 65349cc55cSDimitry Andric Ptr += sizeof(u64); 66349cc55cSDimitry Andric 67349cc55cSDimitry Andric u64 NumSegmentsRecorded = 0; 68349cc55cSDimitry Andric 69*06c3fb27SDimitry Andric for (const auto &Module : Modules) { 70*06c3fb27SDimitry Andric for (const auto &Segment : Module.ranges()) { 71*06c3fb27SDimitry Andric if (Segment.executable) { 72*06c3fb27SDimitry Andric SegmentEntry Entry(Segment.beg, Segment.end, Module.base_address()); 73*06c3fb27SDimitry Andric CHECK(Module.uuid_size() <= MEMPROF_BUILDID_MAX_SIZE); 74*06c3fb27SDimitry Andric Entry.BuildIdSize = Module.uuid_size(); 75*06c3fb27SDimitry Andric memcpy(Entry.BuildId, Module.uuid(), Module.uuid_size()); 764824e7fdSDimitry Andric memcpy(Ptr, &Entry, sizeof(SegmentEntry)); 77349cc55cSDimitry Andric Ptr += sizeof(SegmentEntry); 78349cc55cSDimitry Andric NumSegmentsRecorded++; 79349cc55cSDimitry Andric } 80349cc55cSDimitry Andric } 81*06c3fb27SDimitry Andric } 82349cc55cSDimitry Andric // Store the number of segments we recorded in the space we reserved. 83349cc55cSDimitry Andric *((u64 *)Buffer) = NumSegmentsRecorded; 844824e7fdSDimitry Andric CHECK(ExpectedNumBytes >= static_cast<u64>(Ptr - Buffer) && 85349cc55cSDimitry Andric "Expected num bytes != actual bytes written"); 86349cc55cSDimitry Andric } 87349cc55cSDimitry Andric 88349cc55cSDimitry Andric u64 StackSizeBytes(const Vector<u64> &StackIds) { 89349cc55cSDimitry Andric u64 NumBytesToWrite = sizeof(u64); 90349cc55cSDimitry Andric 91349cc55cSDimitry Andric const u64 NumIds = StackIds.Size(); 92349cc55cSDimitry Andric for (unsigned k = 0; k < NumIds; ++k) { 93349cc55cSDimitry Andric const u64 Id = StackIds[k]; 94349cc55cSDimitry Andric // One entry for the id and then one more for the number of stack pcs. 95349cc55cSDimitry Andric NumBytesToWrite += 2 * sizeof(u64); 96349cc55cSDimitry Andric const StackTrace St = StackDepotGet(Id); 97349cc55cSDimitry Andric 98349cc55cSDimitry Andric CHECK(St.trace != nullptr && St.size > 0 && "Empty stack trace"); 99349cc55cSDimitry Andric for (uptr i = 0; i < St.size && St.trace[i] != 0; i++) { 100349cc55cSDimitry Andric NumBytesToWrite += sizeof(u64); 101349cc55cSDimitry Andric } 102349cc55cSDimitry Andric } 103349cc55cSDimitry Andric return NumBytesToWrite; 104349cc55cSDimitry Andric } 105349cc55cSDimitry Andric 106349cc55cSDimitry Andric // The stack info section uses the following format: 107349cc55cSDimitry Andric // 108349cc55cSDimitry Andric // ---------- Stack Info 109349cc55cSDimitry Andric // Num Entries 110349cc55cSDimitry Andric // ---------- Stack Entry 111349cc55cSDimitry Andric // Num Stacks 112349cc55cSDimitry Andric // PC1 113349cc55cSDimitry Andric // PC2 114349cc55cSDimitry Andric // ... 115349cc55cSDimitry Andric // ---------- 116349cc55cSDimitry Andric void SerializeStackToBuffer(const Vector<u64> &StackIds, 117349cc55cSDimitry Andric const u64 ExpectedNumBytes, char *&Buffer) { 118349cc55cSDimitry Andric const u64 NumIds = StackIds.Size(); 119349cc55cSDimitry Andric char *Ptr = Buffer; 120349cc55cSDimitry Andric Ptr = WriteBytes(static_cast<u64>(NumIds), Ptr); 121349cc55cSDimitry Andric 122349cc55cSDimitry Andric for (unsigned k = 0; k < NumIds; ++k) { 123349cc55cSDimitry Andric const u64 Id = StackIds[k]; 124349cc55cSDimitry Andric Ptr = WriteBytes(Id, Ptr); 125349cc55cSDimitry Andric Ptr += sizeof(u64); // Bump it by u64, we will fill this in later. 126349cc55cSDimitry Andric u64 Count = 0; 127349cc55cSDimitry Andric const StackTrace St = StackDepotGet(Id); 128349cc55cSDimitry Andric for (uptr i = 0; i < St.size && St.trace[i] != 0; i++) { 129349cc55cSDimitry Andric // PCs in stack traces are actually the return addresses, that is, 130349cc55cSDimitry Andric // addresses of the next instructions after the call. 131349cc55cSDimitry Andric uptr pc = StackTrace::GetPreviousInstructionPc(St.trace[i]); 132349cc55cSDimitry Andric Ptr = WriteBytes(static_cast<u64>(pc), Ptr); 133349cc55cSDimitry Andric ++Count; 134349cc55cSDimitry Andric } 135349cc55cSDimitry Andric // Store the count in the space we reserved earlier. 136349cc55cSDimitry Andric *(u64 *)(Ptr - (Count + 1) * sizeof(u64)) = Count; 137349cc55cSDimitry Andric } 138349cc55cSDimitry Andric 1394824e7fdSDimitry Andric CHECK(ExpectedNumBytes >= static_cast<u64>(Ptr - Buffer) && 140349cc55cSDimitry Andric "Expected num bytes != actual bytes written"); 141349cc55cSDimitry Andric } 142349cc55cSDimitry Andric 143349cc55cSDimitry Andric // The MIB section has the following format: 144349cc55cSDimitry Andric // ---------- MIB Info 145349cc55cSDimitry Andric // Num Entries 146349cc55cSDimitry Andric // ---------- MIB Entry 0 147349cc55cSDimitry Andric // Alloc Count 148349cc55cSDimitry Andric // ... 149349cc55cSDimitry Andric // ---------- MIB Entry 1 150349cc55cSDimitry Andric // Alloc Count 151349cc55cSDimitry Andric // ... 152349cc55cSDimitry Andric // ---------- 153349cc55cSDimitry Andric void SerializeMIBInfoToBuffer(MIBMapTy &MIBMap, const Vector<u64> &StackIds, 154349cc55cSDimitry Andric const u64 ExpectedNumBytes, char *&Buffer) { 155349cc55cSDimitry Andric char *Ptr = Buffer; 156349cc55cSDimitry Andric const u64 NumEntries = StackIds.Size(); 157349cc55cSDimitry Andric Ptr = WriteBytes(NumEntries, Ptr); 158349cc55cSDimitry Andric 159349cc55cSDimitry Andric for (u64 i = 0; i < NumEntries; i++) { 160349cc55cSDimitry Andric const u64 Key = StackIds[i]; 161349cc55cSDimitry Andric MIBMapTy::Handle h(&MIBMap, Key, /*remove=*/true, /*create=*/false); 162349cc55cSDimitry Andric CHECK(h.exists()); 163349cc55cSDimitry Andric Ptr = WriteBytes(Key, Ptr); 164349cc55cSDimitry Andric Ptr = WriteBytes((*h)->mib, Ptr); 165349cc55cSDimitry Andric } 166349cc55cSDimitry Andric 1674824e7fdSDimitry Andric CHECK(ExpectedNumBytes >= static_cast<u64>(Ptr - Buffer) && 168349cc55cSDimitry Andric "Expected num bytes != actual bytes written"); 169349cc55cSDimitry Andric } 170349cc55cSDimitry Andric 171349cc55cSDimitry Andric // Format 172349cc55cSDimitry Andric // ---------- Header 173349cc55cSDimitry Andric // Magic 174349cc55cSDimitry Andric // Version 175349cc55cSDimitry Andric // Total Size 176349cc55cSDimitry Andric // Segment Offset 177349cc55cSDimitry Andric // MIB Info Offset 178349cc55cSDimitry Andric // Stack Offset 179349cc55cSDimitry Andric // ---------- Segment Info 180349cc55cSDimitry Andric // Num Entries 181349cc55cSDimitry Andric // ---------- Segment Entry 182349cc55cSDimitry Andric // Start 183349cc55cSDimitry Andric // End 184349cc55cSDimitry Andric // Offset 185349cc55cSDimitry Andric // BuildID 32B 186349cc55cSDimitry Andric // ---------- 187349cc55cSDimitry Andric // ... 1884824e7fdSDimitry Andric // ---------- 1894824e7fdSDimitry Andric // Optional Padding Bytes 190349cc55cSDimitry Andric // ---------- MIB Info 191349cc55cSDimitry Andric // Num Entries 192349cc55cSDimitry Andric // ---------- MIB Entry 193349cc55cSDimitry Andric // Alloc Count 194349cc55cSDimitry Andric // ... 1954824e7fdSDimitry Andric // ---------- 1964824e7fdSDimitry Andric // Optional Padding Bytes 197349cc55cSDimitry Andric // ---------- Stack Info 198349cc55cSDimitry Andric // Num Entries 199349cc55cSDimitry Andric // ---------- Stack Entry 200349cc55cSDimitry Andric // Num Stacks 201349cc55cSDimitry Andric // PC1 202349cc55cSDimitry Andric // PC2 203349cc55cSDimitry Andric // ... 204349cc55cSDimitry Andric // ---------- 2054824e7fdSDimitry Andric // Optional Padding Bytes 206349cc55cSDimitry Andric // ... 207*06c3fb27SDimitry Andric u64 SerializeToRawProfile(MIBMapTy &MIBMap, ArrayRef<LoadedModule> Modules, 208349cc55cSDimitry Andric char *&Buffer) { 2094824e7fdSDimitry Andric // Each section size is rounded up to 8b since the first entry in each section 2104824e7fdSDimitry Andric // is a u64 which holds the number of entries in the section by convention. 211*06c3fb27SDimitry Andric const u64 NumSegmentBytes = RoundUpTo(SegmentSizeBytes(Modules), 8); 212349cc55cSDimitry Andric 213349cc55cSDimitry Andric Vector<u64> StackIds; 214349cc55cSDimitry Andric MIBMap.ForEach(RecordStackId, reinterpret_cast<void *>(&StackIds)); 215349cc55cSDimitry Andric // The first 8b are for the total number of MIB records. Each MIB record is 216349cc55cSDimitry Andric // preceded by a 8b stack id which is associated with stack frames in the next 217349cc55cSDimitry Andric // section. 2184824e7fdSDimitry Andric const u64 NumMIBInfoBytes = RoundUpTo( 2194824e7fdSDimitry Andric sizeof(u64) + StackIds.Size() * (sizeof(u64) + sizeof(MemInfoBlock)), 8); 220349cc55cSDimitry Andric 2214824e7fdSDimitry Andric const u64 NumStackBytes = RoundUpTo(StackSizeBytes(StackIds), 8); 222349cc55cSDimitry Andric 2234824e7fdSDimitry Andric // Ensure that the profile is 8b aligned. We allow for some optional padding 2244824e7fdSDimitry Andric // at the end so that any subsequent profile serialized to the same file does 2254824e7fdSDimitry Andric // not incur unaligned accesses. 2264824e7fdSDimitry Andric const u64 TotalSizeBytes = RoundUpTo( 2274824e7fdSDimitry Andric sizeof(Header) + NumSegmentBytes + NumStackBytes + NumMIBInfoBytes, 8); 228349cc55cSDimitry Andric 229349cc55cSDimitry Andric // Allocate the memory for the entire buffer incl. info blocks. 230349cc55cSDimitry Andric Buffer = (char *)InternalAlloc(TotalSizeBytes); 231349cc55cSDimitry Andric char *Ptr = Buffer; 232349cc55cSDimitry Andric 233349cc55cSDimitry Andric Header header{MEMPROF_RAW_MAGIC_64, 234349cc55cSDimitry Andric MEMPROF_RAW_VERSION, 235349cc55cSDimitry Andric static_cast<u64>(TotalSizeBytes), 236349cc55cSDimitry Andric sizeof(Header), 237349cc55cSDimitry Andric sizeof(Header) + NumSegmentBytes, 238349cc55cSDimitry Andric sizeof(Header) + NumSegmentBytes + NumMIBInfoBytes}; 239349cc55cSDimitry Andric Ptr = WriteBytes(header, Ptr); 240349cc55cSDimitry Andric 241*06c3fb27SDimitry Andric SerializeSegmentsToBuffer(Modules, NumSegmentBytes, Ptr); 242349cc55cSDimitry Andric Ptr += NumSegmentBytes; 243349cc55cSDimitry Andric 244349cc55cSDimitry Andric SerializeMIBInfoToBuffer(MIBMap, StackIds, NumMIBInfoBytes, Ptr); 245349cc55cSDimitry Andric Ptr += NumMIBInfoBytes; 246349cc55cSDimitry Andric 247349cc55cSDimitry Andric SerializeStackToBuffer(StackIds, NumStackBytes, Ptr); 248349cc55cSDimitry Andric 249349cc55cSDimitry Andric return TotalSizeBytes; 250349cc55cSDimitry Andric } 251349cc55cSDimitry Andric 252349cc55cSDimitry Andric } // namespace __memprof 253