From c1dceee50b9ed73d9e6ed8b5dc5e3336316bd07e Mon Sep 17 00:00:00 2001 From: Dean Michael Berris Date: Fri, 31 Aug 2018 17:49:59 +0000 Subject: [PATCH] [XRay] Fix FunctionRecord serialization This change makes the writer implementation more consistent with the way fields are written down to avoid assumptions on bitfield order and padding. We also fix an inconsistency between the type returned by the `delta()` accessor to match the data member it's returning. This is a follow-up to D51289 and D51210. llvm-svn: 341230 --- llvm/include/llvm/XRay/FDRRecords.h | 2 +- llvm/lib/XRay/FDRTraceWriter.cpp | 39 +++++++---------------------- 2 files changed, 10 insertions(+), 31 deletions(-) diff --git a/llvm/include/llvm/XRay/FDRRecords.h b/llvm/include/llvm/XRay/FDRRecords.h index b7858d4f2265..aaa098560c95 100644 --- a/llvm/include/llvm/XRay/FDRRecords.h +++ b/llvm/include/llvm/XRay/FDRRecords.h @@ -244,7 +244,7 @@ public: // properties. RecordTypes recordType() const { return Kind; } int32_t functionId() const { return FuncId; } - uint64_t delta() const { return Delta; } + uint32_t delta() const { return Delta; } Error apply(RecordVisitor &V) override; }; diff --git a/llvm/lib/XRay/FDRTraceWriter.cpp b/llvm/lib/XRay/FDRTraceWriter.cpp index c68e915bbb5b..68c0090aa281 100644 --- a/llvm/lib/XRay/FDRTraceWriter.cpp +++ b/llvm/lib/XRay/FDRTraceWriter.cpp @@ -18,27 +18,6 @@ namespace xray { namespace { -struct alignas(32) FileHeader { - uint16_t Version; - uint16_t Type; - uint32_t BitField; - uint64_t CycleFrequency; - char FreeForm[16]; -}; - -struct MetadataBlob { - uint8_t Type : 1; - uint8_t RecordKind : 7; - char Data[15]; -}; - -struct FunctionDeltaBlob { - uint8_t Type : 1; - uint8_t RecordKind : 3; - int FuncId : 28; - uint32_t TSCDelta; -}; - template struct IndexedWriter { template < class Tuple, @@ -139,16 +118,16 @@ Error FDRTraceWriter::visit(EndBufferRecord &R) { } Error FDRTraceWriter::visit(FunctionRecord &R) { - FunctionDeltaBlob B; - B.Type = 0; - B.RecordKind = static_cast(R.recordType()); - B.FuncId = R.functionId(); - B.TSCDelta = R.delta(); - ArrayRef Bytes(reinterpret_cast(&B), - sizeof(FunctionDeltaBlob)); - OS.write(Bytes); + // Write out the data in "field" order, to be endian-aware. + uint32_t TypeRecordFuncId = uint32_t{R.functionId() & ~uint32_t{0x0Fu << 28}}; + TypeRecordFuncId <<= 3; + TypeRecordFuncId |= static_cast(R.recordType()); + TypeRecordFuncId <<= 1; + TypeRecordFuncId &= ~uint32_t{0x01}; + OS.write(TypeRecordFuncId); + OS.write(R.delta()); return Error::success(); -} +} // namespace xray } // namespace xray } // namespace llvm