1 //===-- ProfiledBinary.cpp - Binary decoder ---------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "ProfiledBinary.h"
10 #include "ErrorHandling.h"
11 #include "ProfileGenerator.h"
12 #include "llvm/ADT/Triple.h"
13 #include "llvm/Demangle/Demangle.h"
14 #include "llvm/IR/DebugInfoMetadata.h"
15 #include "llvm/MC/TargetRegistry.h"
16 #include "llvm/Support/CommandLine.h"
17 #include "llvm/Support/Format.h"
18 #include "llvm/Support/TargetSelect.h"
19 
20 #define DEBUG_TYPE "load-binary"
21 
22 using namespace llvm;
23 using namespace sampleprof;
24 
25 cl::opt<bool> ShowDisassemblyOnly("show-disassembly-only", cl::init(false),
26                                   cl::ZeroOrMore,
27                                   cl::desc("Print disassembled code."));
28 
29 cl::opt<bool> ShowSourceLocations("show-source-locations", cl::init(false),
30                                   cl::ZeroOrMore,
31                                   cl::desc("Print source locations."));
32 
33 static cl::opt<bool>
34     ShowCanonicalFnName("show-canonical-fname", cl::init(false), cl::ZeroOrMore,
35                         cl::desc("Print canonical function name."));
36 
37 static cl::opt<bool> ShowPseudoProbe(
38     "show-pseudo-probe", cl::init(false), cl::ZeroOrMore,
39     cl::desc("Print pseudo probe section and disassembled info."));
40 
41 static cl::opt<bool> UseDwarfCorrelation(
42     "use-dwarf-correlation", cl::init(false), cl::ZeroOrMore,
43     cl::desc("Use dwarf for profile correlation even when binary contains "
44              "pseudo probe."));
45 
46 static cl::list<std::string> DisassembleFunctions(
47     "disassemble-functions", cl::CommaSeparated,
48     cl::desc("List of functions to print disassembly for. Accept demangled "
49              "names only. Only work with show-disassembly-only"));
50 
51 namespace llvm {
52 namespace sampleprof {
53 
54 static const Target *getTarget(const ObjectFile *Obj) {
55   Triple TheTriple = Obj->makeTriple();
56   std::string Error;
57   std::string ArchName;
58   const Target *TheTarget =
59       TargetRegistry::lookupTarget(ArchName, TheTriple, Error);
60   if (!TheTarget)
61     exitWithError(Error, Obj->getFileName());
62   return TheTarget;
63 }
64 
65 void BinarySizeContextTracker::addInstructionForContext(
66     const SampleContextFrameVector &Context, uint32_t InstrSize) {
67   ContextTrieNode *CurNode = &RootContext;
68   bool IsLeaf = true;
69   for (const auto &Callsite : reverse(Context)) {
70     StringRef CallerName = Callsite.FuncName;
71     LineLocation CallsiteLoc = IsLeaf ? LineLocation(0, 0) : Callsite.Location;
72     CurNode = CurNode->getOrCreateChildContext(CallsiteLoc, CallerName);
73     IsLeaf = false;
74   }
75 
76   CurNode->addFunctionSize(InstrSize);
77 }
78 
79 uint32_t
80 BinarySizeContextTracker::getFuncSizeForContext(const SampleContext &Context) {
81   ContextTrieNode *CurrNode = &RootContext;
82   ContextTrieNode *PrevNode = nullptr;
83   SampleContextFrames Frames = Context.getContextFrames();
84   int32_t I = Frames.size() - 1;
85   Optional<uint32_t> Size;
86 
87   // Start from top-level context-less function, traverse down the reverse
88   // context trie to find the best/longest match for given context, then
89   // retrieve the size.
90 
91   while (CurrNode && I >= 0) {
92     // Process from leaf function to callers (added to context).
93     const auto &ChildFrame = Frames[I--];
94     PrevNode = CurrNode;
95     CurrNode =
96         CurrNode->getChildContext(ChildFrame.Location, ChildFrame.FuncName);
97     if (CurrNode && CurrNode->getFunctionSize().hasValue())
98       Size = CurrNode->getFunctionSize().getValue();
99   }
100 
101   // If we traversed all nodes along the path of the context and haven't
102   // found a size yet, pivot to look for size from sibling nodes, i.e size
103   // of inlinee under different context.
104   if (!Size.hasValue()) {
105     if (!CurrNode)
106       CurrNode = PrevNode;
107     while (!Size.hasValue() && CurrNode &&
108            !CurrNode->getAllChildContext().empty()) {
109       CurrNode = &CurrNode->getAllChildContext().begin()->second;
110       if (CurrNode->getFunctionSize().hasValue())
111         Size = CurrNode->getFunctionSize().getValue();
112     }
113   }
114 
115   assert(Size.hasValue() && "We should at least find one context size.");
116   return Size.getValue();
117 }
118 
119 void BinarySizeContextTracker::trackInlineesOptimizedAway(
120     MCPseudoProbeDecoder &ProbeDecoder) {
121   ProbeFrameStack ProbeContext;
122   for (const auto &Child : ProbeDecoder.getDummyInlineRoot().getChildren())
123     trackInlineesOptimizedAway(ProbeDecoder, *Child.second.get(), ProbeContext);
124 }
125 
126 void BinarySizeContextTracker::trackInlineesOptimizedAway(
127     MCPseudoProbeDecoder &ProbeDecoder,
128     MCDecodedPseudoProbeInlineTree &ProbeNode, ProbeFrameStack &ProbeContext) {
129   StringRef FuncName =
130       ProbeDecoder.getFuncDescForGUID(ProbeNode.Guid)->FuncName;
131   ProbeContext.emplace_back(FuncName, 0);
132 
133   // This ProbeContext has a probe, so it has code before inlining and
134   // optimization. Make sure we mark its size as known.
135   if (!ProbeNode.getProbes().empty()) {
136     ContextTrieNode *SizeContext = &RootContext;
137     for (auto &ProbeFrame : reverse(ProbeContext)) {
138       StringRef CallerName = ProbeFrame.first;
139       LineLocation CallsiteLoc(ProbeFrame.second, 0);
140       SizeContext =
141           SizeContext->getOrCreateChildContext(CallsiteLoc, CallerName);
142     }
143     // Add 0 size to make known.
144     SizeContext->addFunctionSize(0);
145   }
146 
147   // DFS down the probe inline tree
148   for (const auto &ChildNode : ProbeNode.getChildren()) {
149     InlineSite Location = ChildNode.first;
150     ProbeContext.back().second = std::get<1>(Location);
151     trackInlineesOptimizedAway(ProbeDecoder, *ChildNode.second.get(), ProbeContext);
152   }
153 
154   ProbeContext.pop_back();
155 }
156 
157 void ProfiledBinary::load() {
158   // Attempt to open the binary.
159   OwningBinary<Binary> OBinary = unwrapOrError(createBinary(Path), Path);
160   Binary &Binary = *OBinary.getBinary();
161 
162   auto *Obj = dyn_cast<ELFObjectFileBase>(&Binary);
163   if (!Obj)
164     exitWithError("not a valid Elf image", Path);
165 
166   TheTriple = Obj->makeTriple();
167   // Current only support X86
168   if (!TheTriple.isX86())
169     exitWithError("unsupported target", TheTriple.getTriple());
170   LLVM_DEBUG(dbgs() << "Loading " << Path << "\n");
171 
172   // Find the preferred load address for text sections.
173   setPreferredTextSegmentAddresses(Obj);
174 
175   // Decode pseudo probe related section
176   decodePseudoProbe(Obj);
177 
178   // Disassemble the text sections.
179   disassemble(Obj);
180 
181   // Track size for optimized inlinees when probe is available
182   if (UsePseudoProbes && TrackFuncContextSize)
183     FuncSizeTracker.trackInlineesOptimizedAway(ProbeDecoder);
184 
185   // Use function start and return address to infer prolog and epilog
186   ProEpilogTracker.inferPrologOffsets(FuncStartOffsetMap);
187   ProEpilogTracker.inferEpilogOffsets(RetAddrs);
188 
189   // TODO: decode other sections.
190 }
191 
192 bool ProfiledBinary::inlineContextEqual(uint64_t Address1, uint64_t Address2) {
193   uint64_t Offset1 = virtualAddrToOffset(Address1);
194   uint64_t Offset2 = virtualAddrToOffset(Address2);
195   const SampleContextFrameVector &Context1 = getFrameLocationStack(Offset1);
196   const SampleContextFrameVector &Context2 = getFrameLocationStack(Offset2);
197   if (Context1.size() != Context2.size())
198     return false;
199   if (Context1.empty())
200     return false;
201   // The leaf frame contains location within the leaf, and it
202   // needs to be remove that as it's not part of the calling context
203   return std::equal(Context1.begin(), Context1.begin() + Context1.size() - 1,
204                     Context2.begin(), Context2.begin() + Context2.size() - 1);
205 }
206 
207 SampleContextFrameVector
208 ProfiledBinary::getExpandedContext(const SmallVectorImpl<uint64_t> &Stack,
209                                    bool &WasLeafInlined) {
210   SampleContextFrameVector ContextVec;
211   // Process from frame root to leaf
212   for (auto Address : Stack) {
213     uint64_t Offset = virtualAddrToOffset(Address);
214     const SampleContextFrameVector &ExpandedContext =
215         getFrameLocationStack(Offset);
216     // An instruction without a valid debug line will be ignored by sample
217     // processing
218     if (ExpandedContext.empty())
219       return SampleContextFrameVector();
220     // Set WasLeafInlined to the size of inlined frame count for the last
221     // address which is leaf
222     WasLeafInlined = (ExpandedContext.size() > 1);
223     ContextVec.append(ExpandedContext);
224   }
225 
226   // Replace with decoded base discriminator
227   for (auto &Frame : ContextVec) {
228     Frame.Location.Discriminator = ProfileGeneratorBase::getBaseDiscriminator(
229         Frame.Location.Discriminator);
230   }
231 
232   assert(ContextVec.size() && "Context length should be at least 1");
233 
234   // Compress the context string except for the leaf frame
235   auto LeafFrame = ContextVec.back();
236   LeafFrame.Location = LineLocation(0, 0);
237   ContextVec.pop_back();
238   CSProfileGenerator::compressRecursionContext(ContextVec);
239   CSProfileGenerator::trimContext(ContextVec);
240   ContextVec.push_back(LeafFrame);
241   return ContextVec;
242 }
243 
244 template <class ELFT>
245 void ProfiledBinary::setPreferredTextSegmentAddresses(const ELFFile<ELFT> &Obj, StringRef FileName) {
246   const auto &PhdrRange = unwrapOrError(Obj.program_headers(), FileName);
247   // FIXME: This should be the page size of the system running profiling.
248   // However such info isn't available at post-processing time, assuming
249   // 4K page now. Note that we don't use EXEC_PAGESIZE from <linux/param.h>
250   // because we may build the tools on non-linux.
251   uint32_t PageSize = 0x1000;
252   for (const typename ELFT::Phdr &Phdr : PhdrRange) {
253     if ((Phdr.p_type == ELF::PT_LOAD) && (Phdr.p_flags & ELF::PF_X)) {
254         // Segments will always be loaded at a page boundary.
255         PreferredTextSegmentAddresses.push_back(Phdr.p_vaddr &
256                                                 ~(PageSize - 1U));
257         TextSegmentOffsets.push_back(Phdr.p_offset & ~(PageSize - 1U));
258       }
259   }
260 
261   if (PreferredTextSegmentAddresses.empty())
262     exitWithError("no executable segment found", FileName);
263 }
264 
265 void ProfiledBinary::setPreferredTextSegmentAddresses(const ELFObjectFileBase *Obj) {
266   if (const auto *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj))
267     setPreferredTextSegmentAddresses(ELFObj->getELFFile(), Obj->getFileName());
268   else if (const auto *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj))
269     setPreferredTextSegmentAddresses(ELFObj->getELFFile(), Obj->getFileName());
270   else if (const auto *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj))
271     setPreferredTextSegmentAddresses(ELFObj->getELFFile(), Obj->getFileName());
272   else if (const auto *ELFObj = cast<ELF64BEObjectFile>(Obj))
273     setPreferredTextSegmentAddresses(ELFObj->getELFFile(), Obj->getFileName());
274   else
275     llvm_unreachable("invalid ELF object format");
276 }
277 
278 void ProfiledBinary::decodePseudoProbe(const ELFObjectFileBase *Obj) {
279   if (UseDwarfCorrelation)
280     return;
281 
282   StringRef FileName = Obj->getFileName();
283   for (section_iterator SI = Obj->section_begin(), SE = Obj->section_end();
284        SI != SE; ++SI) {
285     const SectionRef &Section = *SI;
286     StringRef SectionName = unwrapOrError(Section.getName(), FileName);
287 
288     if (SectionName == ".pseudo_probe_desc") {
289       StringRef Contents = unwrapOrError(Section.getContents(), FileName);
290       if (!ProbeDecoder.buildGUID2FuncDescMap(
291               reinterpret_cast<const uint8_t *>(Contents.data()),
292               Contents.size()))
293         exitWithError("Pseudo Probe decoder fail in .pseudo_probe_desc section");
294     } else if (SectionName == ".pseudo_probe") {
295       StringRef Contents = unwrapOrError(Section.getContents(), FileName);
296       if (!ProbeDecoder.buildAddress2ProbeMap(
297               reinterpret_cast<const uint8_t *>(Contents.data()),
298               Contents.size()))
299         exitWithError("Pseudo Probe decoder fail in .pseudo_probe section");
300       // set UsePseudoProbes flag, used for PerfReader
301       UsePseudoProbes = true;
302     }
303   }
304 
305   if (ShowPseudoProbe)
306     ProbeDecoder.printGUID2FuncDescMap(outs());
307 }
308 
309 bool ProfiledBinary::dissassembleSymbol(std::size_t SI, ArrayRef<uint8_t> Bytes,
310                                         SectionSymbolsTy &Symbols,
311                                         const SectionRef &Section) {
312   std::size_t SE = Symbols.size();
313   uint64_t SectionOffset = Section.getAddress() - getPreferredBaseAddress();
314   uint64_t SectSize = Section.getSize();
315   uint64_t StartOffset = Symbols[SI].Addr - getPreferredBaseAddress();
316   uint64_t NextStartOffset =
317       (SI + 1 < SE) ? Symbols[SI + 1].Addr - getPreferredBaseAddress()
318                     : SectionOffset + SectSize;
319   if (StartOffset >= NextStartOffset)
320     return true;
321 
322   StringRef SymbolName =
323       ShowCanonicalFnName
324           ? FunctionSamples::getCanonicalFnName(Symbols[SI].Name)
325           : Symbols[SI].Name;
326   bool ShowDisassembly =
327       ShowDisassemblyOnly && (DisassembleFunctionSet.empty() ||
328                               DisassembleFunctionSet.count(SymbolName));
329   if (ShowDisassembly)
330     outs() << '<' << SymbolName << ">:\n";
331 
332   auto WarnInvalidInsts = [](uint64_t Start, uint64_t End) {
333     WithColor::warning() << "Invalid instructions at "
334                          << format("%8" PRIx64, Start) << " - "
335                          << format("%8" PRIx64, End) << "\n";
336   };
337 
338   uint64_t Offset = StartOffset;
339   uint64_t EndOffset = 0;
340   // Size of a consecutive invalid instruction range starting from Offset -1
341   // backwards.
342   uint64_t InvalidInstLength = 0;
343   while (Offset < NextStartOffset) {
344     MCInst Inst;
345     uint64_t Size;
346     // Disassemble an instruction.
347     bool Disassembled =
348         DisAsm->getInstruction(Inst, Size, Bytes.slice(Offset - SectionOffset),
349                                Offset + getPreferredBaseAddress(), nulls());
350     if (Size == 0)
351       Size = 1;
352 
353     if (ShowDisassembly) {
354       if (ShowPseudoProbe) {
355         ProbeDecoder.printProbeForAddress(outs(),
356                                           Offset + getPreferredBaseAddress());
357       }
358       outs() << format("%8" PRIx64 ":", Offset + getPreferredBaseAddress());
359       size_t Start = outs().tell();
360       if (Disassembled)
361         IPrinter->printInst(&Inst, Offset + Size, "", *STI.get(), outs());
362       else
363         outs() << "\t<unknown>";
364       if (ShowSourceLocations) {
365         unsigned Cur = outs().tell() - Start;
366         if (Cur < 40)
367           outs().indent(40 - Cur);
368         InstructionPointer IP(this, Offset);
369         outs() << getReversedLocWithContext(
370             symbolize(IP, ShowCanonicalFnName, ShowPseudoProbe));
371       }
372       outs() << "\n";
373     }
374 
375     if (Disassembled) {
376       const MCInstrDesc &MCDesc = MII->get(Inst.getOpcode());
377 
378       // Record instruction size.
379       Offset2InstSizeMap[Offset] = Size;
380 
381       // Populate address maps.
382       CodeAddrOffsets.push_back(Offset);
383       if (MCDesc.isCall())
384         CallAddrs.insert(Offset);
385       else if (MCDesc.isReturn())
386         RetAddrs.insert(Offset);
387 
388       EndOffset = Offset;
389 
390       if (InvalidInstLength) {
391         WarnInvalidInsts(Offset - InvalidInstLength, Offset - 1);
392         InvalidInstLength = 0;
393       }
394     } else {
395       InvalidInstLength += Size;
396     }
397 
398     Offset += Size;
399   }
400 
401   if (InvalidInstLength)
402     WarnInvalidInsts(Offset - InvalidInstLength, Offset - 1);
403 
404   if (ShowDisassembly)
405     outs() << "\n";
406 
407   FuncStartOffsetMap.emplace(StartOffset,
408                              std::make_pair(Symbols[SI].Name.str(), EndOffset));
409   return true;
410 }
411 
412 void ProfiledBinary::setUpDisassembler(const ELFObjectFileBase *Obj) {
413   const Target *TheTarget = getTarget(Obj);
414   std::string TripleName = TheTriple.getTriple();
415   StringRef FileName = Obj->getFileName();
416 
417   MRI.reset(TheTarget->createMCRegInfo(TripleName));
418   if (!MRI)
419     exitWithError("no register info for target " + TripleName, FileName);
420 
421   MCTargetOptions MCOptions;
422   AsmInfo.reset(TheTarget->createMCAsmInfo(*MRI, TripleName, MCOptions));
423   if (!AsmInfo)
424     exitWithError("no assembly info for target " + TripleName, FileName);
425 
426   SubtargetFeatures Features = Obj->getFeatures();
427   STI.reset(
428       TheTarget->createMCSubtargetInfo(TripleName, "", Features.getString()));
429   if (!STI)
430     exitWithError("no subtarget info for target " + TripleName, FileName);
431 
432   MII.reset(TheTarget->createMCInstrInfo());
433   if (!MII)
434     exitWithError("no instruction info for target " + TripleName, FileName);
435 
436   MCContext Ctx(Triple(TripleName), AsmInfo.get(), MRI.get(), STI.get());
437   std::unique_ptr<MCObjectFileInfo> MOFI(
438       TheTarget->createMCObjectFileInfo(Ctx, /*PIC=*/false));
439   Ctx.setObjectFileInfo(MOFI.get());
440   DisAsm.reset(TheTarget->createMCDisassembler(*STI, Ctx));
441   if (!DisAsm)
442     exitWithError("no disassembler for target " + TripleName, FileName);
443 
444   MIA.reset(TheTarget->createMCInstrAnalysis(MII.get()));
445 
446   int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
447   IPrinter.reset(TheTarget->createMCInstPrinter(
448       Triple(TripleName), AsmPrinterVariant, *AsmInfo, *MII, *MRI));
449   IPrinter->setPrintBranchImmAsAddress(true);
450 }
451 
452 void ProfiledBinary::disassemble(const ELFObjectFileBase *Obj) {
453   // Set up disassembler and related components.
454   setUpDisassembler(Obj);
455 
456   // Create a mapping from virtual address to symbol name. The symbols in text
457   // sections are the candidates to dissassemble.
458   std::map<SectionRef, SectionSymbolsTy> AllSymbols;
459   StringRef FileName = Obj->getFileName();
460   for (const SymbolRef &Symbol : Obj->symbols()) {
461     const uint64_t Addr = unwrapOrError(Symbol.getAddress(), FileName);
462     const StringRef Name = unwrapOrError(Symbol.getName(), FileName);
463     section_iterator SecI = unwrapOrError(Symbol.getSection(), FileName);
464     if (SecI != Obj->section_end())
465       AllSymbols[*SecI].push_back(SymbolInfoTy(Addr, Name, ELF::STT_NOTYPE));
466   }
467 
468   // Sort all the symbols. Use a stable sort to stabilize the output.
469   for (std::pair<const SectionRef, SectionSymbolsTy> &SecSyms : AllSymbols)
470     stable_sort(SecSyms.second);
471 
472   DisassembleFunctionSet.insert(DisassembleFunctions.begin(),
473                                 DisassembleFunctions.end());
474   assert((DisassembleFunctionSet.empty() || ShowDisassemblyOnly) &&
475          "Functions to disassemble should be only specified together with "
476          "--show-disassembly-only");
477 
478   if (ShowDisassemblyOnly)
479     outs() << "\nDisassembly of " << FileName << ":\n";
480 
481   // Dissassemble a text section.
482   for (section_iterator SI = Obj->section_begin(), SE = Obj->section_end();
483        SI != SE; ++SI) {
484     const SectionRef &Section = *SI;
485     if (!Section.isText())
486       continue;
487 
488     uint64_t ImageLoadAddr = getPreferredBaseAddress();
489     uint64_t SectionOffset = Section.getAddress() - ImageLoadAddr;
490     uint64_t SectSize = Section.getSize();
491     if (!SectSize)
492       continue;
493 
494     // Register the text section.
495     TextSections.insert({SectionOffset, SectSize});
496 
497     if (ShowDisassemblyOnly) {
498       StringRef SectionName = unwrapOrError(Section.getName(), FileName);
499       outs() << "\nDisassembly of section " << SectionName;
500       outs() << " [" << format("0x%" PRIx64, Section.getAddress()) << ", "
501              << format("0x%" PRIx64, Section.getAddress() + SectSize)
502              << "]:\n\n";
503     }
504 
505     // Get the section data.
506     ArrayRef<uint8_t> Bytes =
507         arrayRefFromStringRef(unwrapOrError(Section.getContents(), FileName));
508 
509     // Get the list of all the symbols in this section.
510     SectionSymbolsTy &Symbols = AllSymbols[Section];
511 
512     // Disassemble symbol by symbol.
513     for (std::size_t SI = 0, SE = Symbols.size(); SI != SE; ++SI) {
514       if (!dissassembleSymbol(SI, Bytes, Symbols, Section))
515         exitWithError("disassembling error", FileName);
516     }
517   }
518 }
519 
520 void ProfiledBinary::setupSymbolizer() {
521   symbolize::LLVMSymbolizer::Options SymbolizerOpts;
522   SymbolizerOpts.PrintFunctions =
523       DILineInfoSpecifier::FunctionNameKind::LinkageName;
524   SymbolizerOpts.Demangle = false;
525   SymbolizerOpts.DefaultArch = TheTriple.getArchName().str();
526   SymbolizerOpts.UseSymbolTable = false;
527   SymbolizerOpts.RelativeAddresses = false;
528   Symbolizer = std::make_unique<symbolize::LLVMSymbolizer>(SymbolizerOpts);
529 }
530 
531 SampleContextFrameVector ProfiledBinary::symbolize(const InstructionPointer &IP,
532                                                    bool UseCanonicalFnName,
533                                                    bool UseProbeDiscriminator) {
534   assert(this == IP.Binary &&
535          "Binary should only symbolize its own instruction");
536   auto Addr = object::SectionedAddress{IP.Offset + getPreferredBaseAddress(),
537                                        object::SectionedAddress::UndefSection};
538   DIInliningInfo InlineStack =
539       unwrapOrError(Symbolizer->symbolizeInlinedCode(Path, Addr), getName());
540 
541   SampleContextFrameVector CallStack;
542   for (int32_t I = InlineStack.getNumberOfFrames() - 1; I >= 0; I--) {
543     const auto &CallerFrame = InlineStack.getFrame(I);
544     if (CallerFrame.FunctionName == "<invalid>")
545       break;
546 
547     StringRef FunctionName(CallerFrame.FunctionName);
548     if (UseCanonicalFnName)
549       FunctionName = FunctionSamples::getCanonicalFnName(FunctionName);
550 
551     uint32_t Discriminator = CallerFrame.Discriminator;
552     uint32_t LineOffset = CallerFrame.Line - CallerFrame.StartLine;
553     if (UseProbeDiscriminator) {
554       LineOffset =
555           PseudoProbeDwarfDiscriminator::extractProbeIndex(Discriminator);
556       Discriminator = 0;
557     } else {
558       // Filter out invalid negative(int type) lineOffset
559       if (LineOffset & 0xffff0000)
560         return SampleContextFrameVector();
561     }
562 
563     LineLocation Line(LineOffset, Discriminator);
564     auto It = NameStrings.insert(FunctionName.str());
565     CallStack.emplace_back(*It.first, Line);
566   }
567 
568   return CallStack;
569 }
570 
571 void ProfiledBinary::computeInlinedContextSizeForRange(uint64_t StartOffset,
572                                                        uint64_t EndOffset) {
573   uint32_t Index = getIndexForOffset(StartOffset);
574   if (CodeAddrOffsets[Index] != StartOffset)
575     WithColor::warning() << "Invalid start instruction at "
576                          << format("%8" PRIx64, StartOffset) << "\n";
577 
578   uint64_t Offset = CodeAddrOffsets[Index];
579   while (Offset <= EndOffset) {
580     const SampleContextFrameVector &SymbolizedCallStack =
581         getFrameLocationStack(Offset, UsePseudoProbes);
582     uint64_t Size = Offset2InstSizeMap[Offset];
583 
584     // Record instruction size for the corresponding context
585     FuncSizeTracker.addInstructionForContext(SymbolizedCallStack, Size);
586 
587     Offset = CodeAddrOffsets[++Index];
588   }
589 }
590 
591 InstructionPointer::InstructionPointer(const ProfiledBinary *Binary,
592                                        uint64_t Address, bool RoundToNext)
593     : Binary(Binary), Address(Address) {
594   Index = Binary->getIndexForAddr(Address);
595   if (RoundToNext) {
596     // we might get address which is not the code
597     // it should round to the next valid address
598     this->Address = Binary->getAddressforIndex(Index);
599   }
600 }
601 
602 void InstructionPointer::advance() {
603   Index++;
604   Address = Binary->getAddressforIndex(Index);
605 }
606 
607 void InstructionPointer::backward() {
608   Index--;
609   Address = Binary->getAddressforIndex(Index);
610 }
611 
612 void InstructionPointer::update(uint64_t Addr) {
613   Address = Addr;
614   Index = Binary->getIndexForAddr(Address);
615 }
616 
617 } // end namespace sampleprof
618 } // end namespace llvm
619