LLVM  9.0.0svn
DIPrinter.cpp
Go to the documentation of this file.
1 //===- lib/DebugInfo/Symbolize/DIPrinter.cpp ------------------------------===//
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 // This file defines the DIPrinter class, which is responsible for printing
10 // structures defined in DebugInfo/DIContext.h
11 //
12 //===----------------------------------------------------------------------===//
13 
15 #include "llvm/ADT/StringRef.h"
17 #include "llvm/Support/ErrorOr.h"
18 #include "llvm/Support/Format.h"
21 #include "llvm/Support/Path.h"
23 #include <algorithm>
24 #include <cmath>
25 #include <cstddef>
26 #include <cstdint>
27 #include <memory>
28 #include <string>
29 
30 namespace llvm {
31 namespace symbolize {
32 
33 // By default, DILineInfo contains "<invalid>" for function/filename it
34 // cannot fetch. We replace it to "??" to make our output closer to addr2line.
35 static const char kDILineInfoBadString[] = "<invalid>";
36 static const char kBadString[] = "??";
37 
38 // Prints source code around in the FileName the Line.
39 void DIPrinter::printContext(const std::string &FileName, int64_t Line) {
40  if (PrintSourceContext <= 0)
41  return;
42 
44  MemoryBuffer::getFile(FileName);
45  if (!BufOrErr)
46  return;
47 
48  std::unique_ptr<MemoryBuffer> Buf = std::move(BufOrErr.get());
49  int64_t FirstLine =
50  std::max(static_cast<int64_t>(1), Line - PrintSourceContext / 2);
51  int64_t LastLine = FirstLine + PrintSourceContext;
52  size_t MaxLineNumberWidth = std::ceil(std::log10(LastLine));
53 
54  for (line_iterator I = line_iterator(*Buf, false);
55  !I.is_at_eof() && I.line_number() <= LastLine; ++I) {
56  int64_t L = I.line_number();
57  if (L >= FirstLine && L <= LastLine) {
58  OS << format_decimal(L, MaxLineNumberWidth);
59  if (L == Line)
60  OS << " >: ";
61  else
62  OS << " : ";
63  OS << *I << "\n";
64  }
65  }
66 }
67 
68 void DIPrinter::print(const DILineInfo &Info, bool Inlined) {
69  if (PrintFunctionNames) {
70  std::string FunctionName = Info.FunctionName;
71  if (FunctionName == kDILineInfoBadString)
72  FunctionName = kBadString;
73 
74  StringRef Delimiter = PrintPretty ? " at " : "\n";
75  StringRef Prefix = (PrintPretty && Inlined) ? " (inlined by) " : "";
76  OS << Prefix << FunctionName << Delimiter;
77  }
78  std::string Filename = Info.FileName;
79  if (Filename == kDILineInfoBadString)
80  Filename = kBadString;
81  else if (Basenames)
82  Filename = llvm::sys::path::filename(Filename);
83  if (!Verbose) {
84  OS << Filename << ":" << Info.Line;
85  if (Style == OutputStyle::LLVM)
86  OS << ":" << Info.Column;
87  OS << "\n";
88  printContext(Filename, Info.Line);
89  return;
90  }
91  OS << " Filename: " << Filename << "\n";
92  if (Info.StartLine)
93  OS << "Function start line: " << Info.StartLine << "\n";
94  OS << " Line: " << Info.Line << "\n";
95  OS << " Column: " << Info.Column << "\n";
96  if (Info.Discriminator)
97  OS << " Discriminator: " << Info.Discriminator << "\n";
98 }
99 
101  print(Info, false);
102  return *this;
103 }
104 
106  uint32_t FramesNum = Info.getNumberOfFrames();
107  if (FramesNum == 0) {
108  print(DILineInfo(), false);
109  return *this;
110  }
111  for (uint32_t i = 0; i < FramesNum; i++)
112  print(Info.getFrame(i), i > 0);
113  return *this;
114 }
115 
117  std::string Name = Global.Name;
118  if (Name == kDILineInfoBadString)
119  Name = kBadString;
120  OS << Name << "\n";
121  OS << Global.Start << " " << Global.Size << "\n";
122  return *this;
123 }
124 
125 } // end namespace symbolize
126 } // end namespace llvm
uint32_t StartLine
Definition: DIContext.h:36
Represents either an error or a value T.
Definition: ErrorOr.h:56
uint32_t Discriminator
Definition: DIContext.h:39
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
std::string FileName
Definition: DIContext.h:31
This class represents lattice values for constants.
Definition: AllocatorList.h:23
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
A forward iterator which reads text lines from a buffer.
Definition: LineIterator.h:31
DIPrinter & operator<<(const DILineInfo &Info)
Definition: DIPrinter.cpp:100
A format-neutral container for source line information.
Definition: DIContext.h:30
uint64_t Start
Definition: DIContext.h:110
uint32_t getNumberOfFrames() const
Definition: DIContext.h:93
Analysis containing CSE Info
Definition: CSEInfo.cpp:20
uint32_t Column
Definition: DIContext.h:35
A format-neutral container for inlined code description.
Definition: DIContext.h:77
static const char kBadString[]
Definition: DIPrinter.cpp:36
uint32_t Line
Definition: DIContext.h:34
std::string Name
Definition: DIContext.h:109
std::string FunctionName
Definition: DIContext.h:32
uint64_t Size
Definition: DIContext.h:111
FormattedNumber format_decimal(int64_t N, unsigned Width)
format_decimal - Output N as a right justified, fixed-width decimal.
Definition: Format.h:210
StringRef filename(StringRef path, Style style=Style::native)
Get filename.
Definition: Path.cpp:564
#define I(x, y, z)
Definition: MD5.cpp:58
static const char kDILineInfoBadString[]
Definition: DIPrinter.cpp:35
Provides ErrorOr<T> smart pointer.
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, int64_t FileSize=-1, bool RequiresNullTerminator=true, bool IsVolatile=false)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful, otherwise returning null.
const DILineInfo & getFrame(unsigned Index) const
Definition: DIContext.h:83
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
reference get()
Definition: ErrorOr.h:156
Container for description of a global variable.
Definition: DIContext.h:108