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