diff --git a/llvm/tools/llvm-profgen/PerfReader.cpp b/llvm/tools/llvm-profgen/PerfReader.cpp --- a/llvm/tools/llvm-profgen/PerfReader.cpp +++ b/llvm/tools/llvm-profgen/PerfReader.cpp @@ -7,15 +7,23 @@ //===----------------------------------------------------------------------===// #include "PerfReader.h" #include "ProfileGenerator.h" +#include "llvm/ADT/Statistic.h" #include "llvm/Support/FileSystem.h" +#define DEBUG_TYPE "perf-reader" + +STATISTIC(NumStackSamplesWithInvalidReturnAddress, + "Number of stack samples with an invalid return address"); + +STATISTIC(NumStackSamples, "Number of stack samples"); + static cl::opt ShowMmapEvents("show-mmap-events", cl::ReallyHidden, cl::init(false), cl::ZeroOrMore, cl::desc("Print binary load events.")); cl::opt SkipSymbolization("skip-symbolization", cl::ReallyHidden, cl::init(false), cl::ZeroOrMore, - cl::desc("Dump the unsumbolized profile to the " + cl::desc("Dump the unsymbolized profile to the " "output file. It will show unwinder " "output for CS profile generation.")); @@ -510,15 +518,24 @@ if (!Binary->addressIsCode(FrameAddr)) break; - // We need to translate return address to call address - // for non-leaf frames + // We need to translate return address to call address for non-leaf frames. if (!CallStack.empty()) { - FrameAddr = Binary->getCallAddrFromFrameAddr(FrameAddr); + auto I = Binary->getIndexForAddr(FrameAddr); + FrameAddr = I ? Binary->getAddressforIndex(I - 1) : 0; + // Stop at an invalid return address caused by bad unwinding. This could + // happen to frame-pointer-based unwinding and the callee functions that + // do not have the frame pointer chain set up. + if (!FrameAddr || !Binary->addressIsCall(FrameAddr)) { + NumStackSamplesWithInvalidReturnAddress++; + break; + } } CallStack.emplace_back(FrameAddr); } + NumStackSamples++; + // Skip other unrelated line, find the next valid LBR line // Note that even for empty call stack, we should skip the address at the // bottom, otherwise the following pass may generate a truncated callstack