1 //===- lib/DebugInfo/Symbolize/DIPrinter.cpp ------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the DIPrinter class, which is responsible for printing
11 // structures defined in DebugInfo/DIContext.h
13 //===----------------------------------------------------------------------===//
15 #include "llvm/DebugInfo/Symbolize/DIPrinter.h"
17 #include "llvm/DebugInfo/DIContext.h"
18 #include "llvm/Support/LineIterator.h"
23 // By default, DILineInfo contains "<invalid>" for function/filename it
24 // cannot fetch. We replace it to "??" to make our output closer to addr2line.
25 static const char kDILineInfoBadString[] = "<invalid>";
26 static const char kBadString[] = "??";
28 // Prints source code around in the FileName the Line.
29 void DIPrinter::printContext(std::string FileName, int64_t Line) {
30 if (PrintSourceContext <= 0)
33 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
34 MemoryBuffer::getFile(FileName);
38 std::unique_ptr<MemoryBuffer> Buf = std::move(BufOrErr.get());
39 int64_t FirstLine = std::max((int64_t)1, Line - PrintSourceContext / 2);
40 int64_t LastLine = FirstLine + PrintSourceContext;
41 size_t MaxLineNumberWidth = std::ceil(std::log10(LastLine));
43 for (line_iterator I = line_iterator(*Buf, false);
44 !I.is_at_eof() && I.line_number() <= LastLine; ++I) {
45 int64_t L = I.line_number();
46 if (L >= FirstLine && L <= LastLine) {
47 OS << format_decimal(L, MaxLineNumberWidth);
57 void DIPrinter::print(const DILineInfo &Info, bool Inlined) {
58 if (PrintFunctionNames) {
59 std::string FunctionName = Info.FunctionName;
60 if (FunctionName == kDILineInfoBadString)
61 FunctionName = kBadString;
63 StringRef Delimiter = (PrintPretty == true) ? " at " : "\n";
64 StringRef Prefix = (PrintPretty && Inlined) ? " (inlined by) " : "";
65 OS << Prefix << FunctionName << Delimiter;
67 std::string Filename = Info.FileName;
68 if (Filename == kDILineInfoBadString)
69 Filename = kBadString;
70 OS << Filename << ":" << Info.Line << ":" << Info.Column << "\n";
71 printContext(Filename, Info.Line);
74 DIPrinter &DIPrinter::operator<<(const DILineInfo &Info) {
79 DIPrinter &DIPrinter::operator<<(const DIInliningInfo &Info) {
80 uint32_t FramesNum = Info.getNumberOfFrames();
82 print(DILineInfo(), false);
85 for (uint32_t i = 0; i < FramesNum; i++)
86 print(Info.getFrame(i), i > 0);
90 DIPrinter &DIPrinter::operator<<(const DIGlobal &Global) {
91 std::string Name = Global.Name;
92 if (Name == kDILineInfoBadString)
95 OS << Global.Start << " " << Global.Size << "\n";