30#define DEBUG_TYPE "mcpseudoprobe"
54 assert((LastProbe || IsSentinel) &&
55 "Last probe should not be null for non-sentinel probes");
64 assert(
Type <= 0xF &&
"Probe type too big to encode, exceeding 15");
68 assert(NewAttributes <= 0x7 &&
69 "Probe attributes too big to encode, exceeding 7");
84 F->makeLEB(
true, AddrDelta);
119 if (InlineStack.
empty()) {
129 if (!InlineStack.
empty()) {
130 auto Iter = InlineStack.
begin();
131 auto Index = std::get<1>(*Iter);
133 for (; Iter != InlineStack.
end(); Iter++) {
135 Cur = Cur->getOrAddNode(
InlineSite(std::get<0>(*Iter), Index));
136 Index = std::get<1>(*Iter);
141 Cur->Probes.push_back(Probe);
148 dbgs() <<
"Group [\n";
151 assert(!
isRoot() &&
"Root should be handled separately");
162 bool NeedSentinel =
false;
165 "Starting probe of a top-level function should be a sentinel probe");
176 LastProbe->
emit(MCOS,
nullptr);
179 for (
const auto &Probe :
Probes) {
180 Probe.emit(MCOS, LastProbe);
186 using InlineeType = std::pair<InlineSite, MCPseudoProbeInlineTree *>;
187 std::vector<InlineeType> Inlinees;
189 Inlinees.emplace_back(Child.first, Child.second.get());
192 for (
const auto &Inlinee : Inlinees) {
197 dbgs() <<
"InlineSite: " << std::get<1>(Inlinee.first) <<
"\n";
200 Inlinee.second->emit(MCOS, LastProbe);
213 Vec.
reserve(MCProbeDivisions.size());
214 for (
auto &ProbeSec : MCProbeDivisions)
217 I.value().setOrdinal(
I.index());
219 return A.first->getSection().getOrdinal() <
220 B.first->getSection().getOrdinal();
222 for (
auto [FuncSym, RootPtr] : Vec) {
223 const auto &Root = *RootPtr;
224 if (
auto *S = Ctx.getObjectFileInfo()->getPseudoProbeSection(
225 FuncSym->getSection())) {
231 using InlineeType = std::pair<InlineSite, MCPseudoProbeInlineTree *>;
232 std::vector<InlineeType> Inlinees;
233 for (
const auto &Child : Root.getChildren())
234 Inlinees.emplace_back(Child.first, Child.second.get());
237 for (
const auto &Inlinee : Inlinees) {
245 Inlinee.second->emit(MCOS, Probe);
261 if (ProbeSections.empty())
267 ProbeSections.emit(MCOS);
272 auto It = GUID2FuncMAP.
find(GUID);
273 assert(It != GUID2FuncMAP.end() &&
274 "Probe function must exist for a valid GUID");
297 std::reverse(ContextStack.
begin() + Begin, ContextStack.
end());
302 std::ostringstream OContextStr;
305 for (
auto &Cxt : ContextStack) {
306 if (OContextStr.str().size())
307 OContextStr <<
" @ ";
308 OContextStr << Cxt.first.str() <<
":" << Cxt.second;
310 return OContextStr.str();
318 bool ShowName)
const {
322 OS << FuncName.
str() <<
" ";
326 OS <<
"Index: " <<
Index <<
" ";
331 if (InlineContextStr.size()) {
333 OS << InlineContextStr;
338template <
typename T>
ErrorOr<T> MCPseudoProbeDecoder::readUnencodedNumber() {
339 if (
Data +
sizeof(
T) > End) {
340 return std::error_code();
346template <
typename T>
ErrorOr<T> MCPseudoProbeDecoder::readUnsignedNumber() {
347 unsigned NumBytesRead = 0;
349 if (Val > std::numeric_limits<T>::max() || (Data + NumBytesRead > End)) {
350 return std::error_code();
352 Data += NumBytesRead;
353 return ErrorOr<T>(
static_cast<T>(Val));
356template <
typename T>
ErrorOr<T> MCPseudoProbeDecoder::readSignedNumber() {
357 unsigned NumBytesRead = 0;
359 if (Val > std::numeric_limits<T>::max() || (Data + NumBytesRead > End)) {
360 return std::error_code();
362 Data += NumBytesRead;
363 return ErrorOr<T>(
static_cast<T>(Val));
367 StringRef Str(
reinterpret_cast<const char *
>(Data),
Size);
368 if (Data +
Size > End) {
369 return std::error_code();
372 return ErrorOr<StringRef>(Str);
395 if (!readUnencodedNumber<uint64_t>())
398 if (!readUnencodedNumber<uint64_t>())
401 auto ErrorOrNameSize = readUnsignedNumber<uint32_t>();
402 if (!ErrorOrNameSize)
405 if (!readString(*ErrorOrNameSize))
409 assert(Data == End &&
"Have unprocessed data in pseudo_probe_desc section");
410 GUID2FuncDescMap.reserve(FuncDescCount);
424 GUID2FuncDescMap.emplace_back(
425 GUID, Hash, IsMMapped ? Name : Name.copy(FuncNameAllocator));
427 assert(Data == End &&
"Have unprocessed data in pseudo_probe_desc section");
428 assert(GUID2FuncDescMap.size() == FuncDescCount &&
429 "Mismatching function description count pre- and post-parsing");
430 llvm::sort(GUID2FuncDescMap, [](
const auto &LHS,
const auto &RHS) {
431 return LHS.FuncGUID < RHS.FuncGUID;
436template <
bool IsTopLevelFunc>
439 const Uint64Set &GuidFilter,
const Uint64Map &FuncStartAddrs,
445 if (IsTopLevelFunc) {
447 Index = CurChildIndex;
457 if (IsTopLevelFunc && !GuidFilter.empty() && !GuidFilter.count(
Guid))
466 if (IsTopLevelFunc && !EncodingIsAddrBased) {
467 if (
auto V = FuncStartAddrs.lookup(
Guid))
475 uint32_t CurrentProbeCount = 0;
477 uint32_t ChildrenToProcess =
480 for (std::size_t
I = 0;
I < NodeCount;
I++) {
487 uint8_t Attr = (
Value & 0x70) >> 4;
498 if (
auto V = FuncStartAddrs.lookup(Addr))
505 EncodingIsAddrBased =
true;
526 InlineTreeVec.resize(InlineTreeVec.size() + ChildrenToProcess);
530 for (uint32_t
I = 0;
I < ChildrenToProcess;
I++) {
536template <
bool IsTopLevelFunc>
542 if (!readUnsignedNumber<uint32_t>())
546 auto ErrorOrCurGuid = readUnencodedNumber<uint64_t>();
552 if (IsTopLevelFunc) {
560 auto ErrorOrNodeCount = readUnsignedNumber<uint32_t>();
561 if (!ErrorOrNodeCount)
563 uint32_t NodeCount = std::move(*ErrorOrNodeCount);
567 auto ErrorOrCurChildrenToProcess = readUnsignedNumber<uint32_t>();
568 if (!ErrorOrCurChildrenToProcess)
570 uint32_t ChildrenToProcess = std::move(*ErrorOrCurChildrenToProcess);
573 for (std::size_t
I = 0;
I < NodeCount;
I++) {
575 if (!readUnsignedNumber<uint32_t>())
579 auto ErrorOrValue = readUnencodedNumber<uint8_t>();
587 if (!readSignedNumber<int64_t>())
591 if (!readUnencodedNumber<int64_t>())
597 if (!readUnsignedNumber<uint32_t>())
605 ProbeCount += CurrentProbeCount;
606 InlinedCount += ChildrenToProcess;
625 bool Discard =
false;
629 TopLevelFuncs += !Discard;
631 assert(Data == End &&
"Have unprocessed data in pseudo_probe section");
632 PseudoProbeVec.reserve(ProbeCount);
633 InlineTreeVec.reserve(InlinedCount);
636 InlineTreeVec.resize(TopLevelFuncs);
645 &DummyInlineRoot, LastAddr, GuidFilter, FuncStartAddrs, CurChildIndex);
646 assert(Data == End &&
"Have unprocessed data in pseudo_probe section");
647 assert(PseudoProbeVec.size() == ProbeCount &&
648 "Mismatching probe count pre- and post-parsing");
649 assert(InlineTreeVec.size() == InlinedCount &&
650 "Mismatching function records count pre- and post-parsing");
652 std::vector<std::pair<uint64_t, uint32_t>> SortedA2P(ProbeCount);
654 SortedA2P[
I] = {Probe.getAddress(),
I};
656 Address2ProbesMap.reserve(ProbeCount);
658 Address2ProbesMap.emplace_back(PseudoProbeVec[
I]);
664 OS <<
"Pseudo Probe Desc:\n";
665 for (
auto &
I : GUID2FuncDescMap)
673 Probe.print(OS, GUID2FuncDescMap,
true);
683 OS <<
"Address:\t" <<
Address <<
'\n';
686 Probe.print(OS, GUID2FuncDescMap,
true);
694 if (Probe.isCall()) {
716 auto It = GUID2FuncDescMap.find(GUID);
717 assert(It != GUID2FuncDescMap.end() &&
"Function descriptor doesn't exist");
724 bool IncludeLeaf)
const {
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static const MCExpr * buildSymbolDiff(MCObjectStreamer &OS, const MCSymbol *A, const MCSymbol *B, SMLoc Loc)
static const char * PseudoProbeTypeStr[3]
static StringRef getProbeFNameForGUID(const GUIDProbeFunctionMap &GUID2FuncMAP, uint64_t GUID)
static const MCExpr * buildSymbolDiff(MCObjectStreamer *MCOS, const MCSymbol *A, const MCSymbol *B)
Represents either an error or a value T.
auto find(uint64_t GUID) const
static LLVM_ABI const MCBinaryExpr * create(Opcode Op, const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx, SMLoc Loc=SMLoc())
Context object for machine code objects.
MCPseudoProbeTable & getMCPseudoProbeTable()
InlineSite getInlineSite() const
bool hasInlineSite() const
void setProbes(MutableArrayRef< MCDecodedPseudoProbe > ProbesRef)
LLVM_ABI void print(raw_ostream &OS, const GUIDProbeFunctionMap &GUID2FuncMAP, bool ShowName) const
LLVM_ABI uint64_t getGuid() const
LLVM_ABI std::string getInlineContextStr(const GUIDProbeFunctionMap &GUID2FuncMAP) const
MCDecodedPseudoProbeInlineTree * getInlineTreeNode() const
LLVM_ABI void getInlineContext(SmallVectorImpl< MCPseudoProbeFrameLocation > &ContextStack, const GUIDProbeFunctionMap &GUID2FuncMAP) const
Base class for the full range of assembler expressions which are needed for parsing.
Streaming object file generation interface.
MCAssembler & getAssembler()
MCAssembler * getAssemblerPtr() override
uint32_t getIndex() const
uint8_t getAttributes() const
DenseSet< uint64_t > Uint64Set
LLVM_ABI bool buildAddress2ProbeMap(const uint8_t *Start, std::size_t Size, const Uint64Set &GuildFilter, const Uint64Map &FuncStartAddrs)
LLVM_ABI void printProbesForAllAddresses(raw_ostream &OS)
LLVM_ABI void printGUID2FuncDescMap(raw_ostream &OS)
DenseMap< uint64_t, uint64_t > Uint64Map
LLVM_ABI bool buildGUID2FuncDescMap(const uint8_t *Start, std::size_t Size, bool IsMMapped=false)
LLVM_ABI void printProbeForAddress(raw_ostream &OS, uint64_t Address)
LLVM_ABI void getInlineContextForProbe(const MCDecodedPseudoProbe *Probe, SmallVectorImpl< MCPseudoProbeFrameLocation > &InlineContextStack, bool IncludeLeaf) const
LLVM_ABI const MCPseudoProbeFuncDesc * getInlinerDescForProbe(const MCDecodedPseudoProbe *Probe) const
bool countRecords(bool &Discard, uint32_t &ProbeCount, uint32_t &InlinedCount, const Uint64Set &GuidFilter)
LLVM_ABI const MCDecodedPseudoProbe * getCallProbeForAddr(uint64_t Address) const
LLVM_ABI const MCPseudoProbeFuncDesc * getFuncDescForGUID(uint64_t GUID) const
std::unordered_map< InlineSite, std::unique_ptr< MCPseudoProbeInlineTree >, InlineSiteHash > Children
MCPseudoProbeInlineTree * getOrAddNode(const InlineSite &Site)
std::vector< MCPseudoProbe > Probes
MCPseudoProbeInlineTreeBase< std::vector< MCPseudoProbe >, MCPseudoProbeInlineTree, std::unordered_map< InlineSite, std::unique_ptr< MCPseudoProbeInlineTree >, InlineSiteHash > > * Parent
InlinedProbeTreeMap & getChildren()
LLVM_ABI void emit(MCObjectStreamer *MCOS, const MCPseudoProbe *&LastProbe)
LLVM_ABI void addPseudoProbe(const MCPseudoProbe &Probe, const MCPseudoProbeInlineStack &InlineStack)
LLVM_ABI void emit(MCObjectStreamer *MCOS)
MCPseudoProbeSections & getProbeSections()
static int DdgPrintIndent
static LLVM_ABI void emit(MCObjectStreamer *MCOS)
Instances of this class represent a pseudo probe instance for a pseudo probe table entry,...
MCSymbol * getLabel() const
LLVM_ABI void emit(MCObjectStreamer *MCOS, const MCPseudoProbe *LastProbe) const
MCPseudoProbe(MCSymbol *Label, uint64_t Guid, uint64_t Index, uint64_t Type, uint64_t Attributes, uint32_t Discriminator)
MCFragment * getCurrentFragment() const
MCContext & getContext() const
unsigned emitULEB128IntValue(uint64_t Value, unsigned PadTo=0)
Special case of EmitULEB128Value that avoids the client having to pass in a MCExpr for constant integ...
void emitInt64(uint64_t Value)
virtual void switchSection(MCSection *Section, uint32_t Subsec=0)
Set the current section where code is being emitted to Section.
unsigned emitSLEB128IntValue(int64_t Value)
Special case of EmitSLEB128Value that avoids the client having to pass in a MCExpr for constant integ...
void emitInt8(uint64_t Value)
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx, SMLoc Loc=SMLoc())
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
MutableArrayRef< T > take_back(size_t N=1) const
Return a copy of *this with only the last N elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
reference emplace_back(ArgTypes &&... Args)
void reserve(size_type N)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
std::string str() const
str - Get the contents as an std::string.
LLVM Value Representation.
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
This class implements an extremely fast bulk output stream that can only output to a stream.
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
constexpr size_t NameSize
uint64_t MD5Hash(const FunctionId &Obj)
value_type readNext(const CharT *&memory, endianness endian)
Read a value of a particular endianness from a buffer, and increment the buffer past that value.
This is an optimization pass for GlobalISel generic memory operations.
FunctionAddr VTableAddr Value
static bool isSentinelProbe(uint32_t Flags)
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
std::tuple< uint64_t, uint32_t > InlineSite
uint64_t decodeULEB128(const uint8_t *p, unsigned *n=nullptr, const uint8_t *end=nullptr, const char **error=nullptr)
Utility function to decode a ULEB128 value.
int64_t decodeSLEB128(const uint8_t *p, unsigned *n=nullptr, const uint8_t *end=nullptr, const char **error=nullptr)
Utility function to decode a SLEB128 value.
std::pair< StringRef, uint32_t > MCPseudoProbeFrameLocation
void sort(IteratorTy Start, IteratorTy End)
SmallVector< InlineSite, 8 > MCPseudoProbeInlineStack
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
static bool hasDiscriminator(uint32_t Flags)
MutableArrayRef(T &OneElt) -> MutableArrayRef< T >
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Expected< T > errorOrToExpected(ErrorOr< T > &&EO)
Convert an ErrorOr<T> to an Expected<T>.
auto make_second_range(ContainerTy &&c)
Given a container of pairs, return a range over the second elements.
LLVM_ABI void print(raw_ostream &OS)
Function object to check whether the first component of a container supported by std::get (like std::...