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 14 #include "llvm/DebugInfo/Symbolize/DIPrinter.h" 15 #include "llvm/ADT/StringRef.h" 16 #include "llvm/DebugInfo/DIContext.h" 17 #include "llvm/Support/ErrorOr.h" 18 #include "llvm/Support/Format.h" 19 #include "llvm/Support/MemoryBuffer.h" 20 #include "llvm/Support/raw_ostream.h" 21 #include <algorithm> 22 #include <cmath> 23 #include <cstddef> 24 #include <cstdint> 25 #include <memory> 26 #include <string> 27 28 namespace llvm { 29 namespace symbolize { 30 31 class SourceCode { 32 std::unique_ptr<MemoryBuffer> MemBuf; 33 34 Optional<StringRef> load(StringRef FileName, 35 const Optional<StringRef> &EmbeddedSource) { 36 if (Lines <= 0) 37 return None; 38 39 if (EmbeddedSource) 40 return EmbeddedSource; 41 else { 42 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr = 43 MemoryBuffer::getFile(FileName); 44 if (!BufOrErr) 45 return None; 46 MemBuf = std::move(*BufOrErr); 47 return MemBuf->getBuffer(); 48 } 49 } 50 51 Optional<StringRef> pruneSource(const Optional<StringRef> &Source) { 52 if (!Source) 53 return None; 54 size_t FirstLinePos = StringRef::npos, Pos = 0; 55 for (int64_t L = 1; L <= LastLine; ++L, ++Pos) { 56 if (L == FirstLine) 57 FirstLinePos = Pos; 58 Pos = Source->find('\n', Pos); 59 if (Pos == StringRef::npos) 60 break; 61 } 62 if (FirstLinePos == StringRef::npos) 63 return None; 64 return Source->substr(FirstLinePos, (Pos == StringRef::npos) 65 ? StringRef::npos 66 : Pos - FirstLinePos); 67 } 68 69 public: 70 const int64_t Line; 71 const int Lines; 72 const int64_t FirstLine; 73 const int64_t LastLine; 74 const Optional<StringRef> PrunedSource; 75 76 SourceCode( 77 StringRef FileName, int64_t Line, int Lines, 78 const Optional<StringRef> &EmbeddedSource = Optional<StringRef>(None)) 79 : Line(Line), Lines(Lines), 80 FirstLine(std::max(static_cast<int64_t>(1), Line - Lines / 2)), 81 LastLine(FirstLine + Lines - 1), 82 PrunedSource(pruneSource(load(FileName, EmbeddedSource))) {} 83 84 void format(raw_ostream &OS) { 85 if (!PrunedSource) 86 return; 87 size_t MaxLineNumberWidth = std::ceil(std::log10(LastLine)); 88 int64_t L = FirstLine; 89 for (size_t Pos = 0; Pos < PrunedSource->size(); ++L) { 90 size_t PosEnd = PrunedSource->find('\n', Pos); 91 StringRef String = PrunedSource->substr( 92 Pos, (PosEnd == StringRef::npos) ? StringRef::npos : (PosEnd - Pos)); 93 if (String.endswith("\r")) 94 String = String.drop_back(1); 95 OS << format_decimal(L, MaxLineNumberWidth); 96 if (L == Line) 97 OS << " >: "; 98 else 99 OS << " : "; 100 OS << String << '\n'; 101 if (PosEnd == StringRef::npos) 102 break; 103 Pos = PosEnd + 1; 104 } 105 } 106 }; 107 108 void PlainPrinterBase::printHeader(uint64_t Address) { 109 if (Config.PrintAddress) { 110 OS << "0x"; 111 OS.write_hex(Address); 112 StringRef Delimiter = Config.Pretty ? ": " : "\n"; 113 OS << Delimiter; 114 } 115 } 116 117 // Prints source code around in the FileName the Line. 118 void PlainPrinterBase::printContext(SourceCode SourceCode) { 119 SourceCode.format(OS); 120 } 121 122 void PlainPrinterBase::printFunctionName(StringRef FunctionName, bool Inlined) { 123 if (Config.PrintFunctions) { 124 if (FunctionName == DILineInfo::BadString) 125 FunctionName = DILineInfo::Addr2LineBadString; 126 StringRef Delimiter = Config.Pretty ? " at " : "\n"; 127 StringRef Prefix = (Config.Pretty && Inlined) ? " (inlined by) " : ""; 128 OS << Prefix << FunctionName << Delimiter; 129 } 130 } 131 132 void LLVMPrinter::printSimpleLocation(StringRef Filename, 133 const DILineInfo &Info) { 134 OS << Filename << ':' << Info.Line << ':' << Info.Column << '\n'; 135 printContext( 136 SourceCode(Filename, Info.Line, Config.SourceContextLines, Info.Source)); 137 } 138 139 void GNUPrinter::printSimpleLocation(StringRef Filename, 140 const DILineInfo &Info) { 141 OS << Filename << ':' << Info.Line; 142 if (Info.Discriminator) 143 OS << " (discriminator " << Info.Discriminator << ')'; 144 OS << '\n'; 145 printContext( 146 SourceCode(Filename, Info.Line, Config.SourceContextLines, Info.Source)); 147 } 148 149 void PlainPrinterBase::printVerbose(StringRef Filename, 150 const DILineInfo &Info) { 151 OS << " Filename: " << Filename << '\n'; 152 if (Info.StartLine) { 153 OS << " Function start filename: " << Info.StartFileName << '\n'; 154 OS << " Function start line: " << Info.StartLine << '\n'; 155 } 156 printStartAddress(Info); 157 OS << " Line: " << Info.Line << '\n'; 158 OS << " Column: " << Info.Column << '\n'; 159 if (Info.Discriminator) 160 OS << " Discriminator: " << Info.Discriminator << '\n'; 161 } 162 163 void LLVMPrinter::printStartAddress(const DILineInfo &Info) { 164 if (Info.StartAddress) { 165 OS << " Function start address: 0x"; 166 OS.write_hex(*Info.StartAddress); 167 OS << '\n'; 168 } 169 } 170 171 void LLVMPrinter::printFooter() { OS << '\n'; } 172 173 void PlainPrinterBase::print(const DILineInfo &Info, bool Inlined) { 174 printFunctionName(Info.FunctionName, Inlined); 175 StringRef Filename = Info.FileName; 176 if (Filename == DILineInfo::BadString) 177 Filename = DILineInfo::Addr2LineBadString; 178 if (Config.Verbose) 179 printVerbose(Filename, Info); 180 else 181 printSimpleLocation(Filename, Info); 182 } 183 184 void PlainPrinterBase::print(const Request &Request, const DILineInfo &Info) { 185 printHeader(*Request.Address); 186 print(Info, false); 187 printFooter(); 188 } 189 190 void PlainPrinterBase::print(const Request &Request, 191 const DIInliningInfo &Info) { 192 printHeader(*Request.Address); 193 uint32_t FramesNum = Info.getNumberOfFrames(); 194 if (FramesNum == 0) 195 print(DILineInfo(), false); 196 else 197 for (uint32_t I = 0; I < FramesNum; ++I) 198 print(Info.getFrame(I), I > 0); 199 printFooter(); 200 } 201 202 void PlainPrinterBase::print(const Request &Request, const DIGlobal &Global) { 203 printHeader(*Request.Address); 204 StringRef Name = Global.Name; 205 if (Name == DILineInfo::BadString) 206 Name = DILineInfo::Addr2LineBadString; 207 OS << Name << "\n"; 208 OS << Global.Start << " " << Global.Size << "\n"; 209 printFooter(); 210 } 211 212 void PlainPrinterBase::print(const Request &Request, 213 const std::vector<DILocal> &Locals) { 214 printHeader(*Request.Address); 215 if (Locals.empty()) 216 OS << DILineInfo::Addr2LineBadString << '\n'; 217 else 218 for (const DILocal &L : Locals) { 219 if (L.FunctionName.empty()) 220 OS << DILineInfo::Addr2LineBadString; 221 else 222 OS << L.FunctionName; 223 OS << '\n'; 224 225 if (L.Name.empty()) 226 OS << DILineInfo::Addr2LineBadString; 227 else 228 OS << L.Name; 229 OS << '\n'; 230 231 if (L.DeclFile.empty()) 232 OS << DILineInfo::Addr2LineBadString; 233 else 234 OS << L.DeclFile; 235 236 OS << ':' << L.DeclLine << '\n'; 237 238 if (L.FrameOffset) 239 OS << *L.FrameOffset; 240 else 241 OS << DILineInfo::Addr2LineBadString; 242 OS << ' '; 243 244 if (L.Size) 245 OS << *L.Size; 246 else 247 OS << DILineInfo::Addr2LineBadString; 248 OS << ' '; 249 250 if (L.TagOffset) 251 OS << *L.TagOffset; 252 else 253 OS << DILineInfo::Addr2LineBadString; 254 OS << '\n'; 255 } 256 printFooter(); 257 } 258 259 void PlainPrinterBase::printInvalidCommand(const Request &Request, 260 StringRef Command) { 261 OS << Command << '\n'; 262 } 263 264 bool PlainPrinterBase::printError(const Request &Request, 265 const ErrorInfoBase &ErrorInfo, 266 StringRef ErrorBanner) { 267 ES << ErrorBanner; 268 ErrorInfo.log(ES); 269 ES << '\n'; 270 // Print an empty struct too. 271 return true; 272 } 273 274 static std::string toHex(uint64_t V) { 275 return ("0x" + Twine::utohexstr(V)).str(); 276 } 277 278 static json::Object toJSON(const Request &Request, StringRef ErrorMsg = "") { 279 json::Object Json({{"ModuleName", Request.ModuleName.str()}}); 280 if (Request.Address) 281 Json["Address"] = toHex(*Request.Address); 282 if (!ErrorMsg.empty()) 283 Json["Error"] = json::Object({{"Message", ErrorMsg.str()}}); 284 return Json; 285 } 286 287 void JSONPrinter::print(const Request &Request, const DILineInfo &Info) { 288 DIInliningInfo InliningInfo; 289 InliningInfo.addFrame(Info); 290 print(Request, InliningInfo); 291 } 292 293 void JSONPrinter::print(const Request &Request, const DIInliningInfo &Info) { 294 json::Array Array; 295 for (uint32_t I = 0, N = Info.getNumberOfFrames(); I < N; ++I) { 296 const DILineInfo &LineInfo = Info.getFrame(I); 297 json::Object Object( 298 {{"FunctionName", LineInfo.FunctionName != DILineInfo::BadString 299 ? LineInfo.FunctionName 300 : ""}, 301 {"StartFileName", LineInfo.StartFileName != DILineInfo::BadString 302 ? LineInfo.StartFileName 303 : ""}, 304 {"StartLine", LineInfo.StartLine}, 305 {"StartAddress", 306 LineInfo.StartAddress ? toHex(*LineInfo.StartAddress) : ""}, 307 {"FileName", 308 LineInfo.FileName != DILineInfo::BadString ? LineInfo.FileName : ""}, 309 {"Line", LineInfo.Line}, 310 {"Column", LineInfo.Column}, 311 {"Discriminator", LineInfo.Discriminator}}); 312 SourceCode SourceCode(LineInfo.FileName, LineInfo.Line, 313 Config.SourceContextLines, LineInfo.Source); 314 std::string FormattedSource; 315 raw_string_ostream Stream(FormattedSource); 316 SourceCode.format(Stream); 317 if (!FormattedSource.empty()) 318 Object["Source"] = std::move(FormattedSource); 319 Array.push_back(std::move(Object)); 320 } 321 json::Object Json = toJSON(Request); 322 Json["Symbol"] = std::move(Array); 323 if (ObjectList) 324 ObjectList->push_back(std::move(Json)); 325 else 326 printJSON(std::move(Json)); 327 } 328 329 void JSONPrinter::print(const Request &Request, const DIGlobal &Global) { 330 json::Object Data( 331 {{"Name", Global.Name != DILineInfo::BadString ? Global.Name : ""}, 332 {"Start", toHex(Global.Start)}, 333 {"Size", toHex(Global.Size)}}); 334 json::Object Json = toJSON(Request); 335 Json["Data"] = std::move(Data); 336 if (ObjectList) 337 ObjectList->push_back(std::move(Json)); 338 else 339 printJSON(std::move(Json)); 340 } 341 342 void JSONPrinter::print(const Request &Request, 343 const std::vector<DILocal> &Locals) { 344 json::Array Frame; 345 for (const DILocal &Local : Locals) { 346 json::Object FrameObject( 347 {{"FunctionName", Local.FunctionName}, 348 {"Name", Local.Name}, 349 {"DeclFile", Local.DeclFile}, 350 {"DeclLine", int64_t(Local.DeclLine)}, 351 {"Size", Local.Size ? toHex(*Local.Size) : ""}, 352 {"TagOffset", Local.TagOffset ? toHex(*Local.TagOffset) : ""}}); 353 if (Local.FrameOffset) 354 FrameObject["FrameOffset"] = *Local.FrameOffset; 355 Frame.push_back(std::move(FrameObject)); 356 } 357 json::Object Json = toJSON(Request); 358 Json["Frame"] = std::move(Frame); 359 if (ObjectList) 360 ObjectList->push_back(std::move(Json)); 361 else 362 printJSON(std::move(Json)); 363 } 364 365 void JSONPrinter::printInvalidCommand(const Request &Request, 366 StringRef Command) { 367 printError(Request, 368 StringError("unable to parse arguments: " + Command, 369 std::make_error_code(std::errc::invalid_argument)), 370 ""); 371 } 372 373 bool JSONPrinter::printError(const Request &Request, 374 const ErrorInfoBase &ErrorInfo, 375 StringRef ErrorBanner) { 376 json::Object Json = toJSON(Request, ErrorInfo.message()); 377 if (ObjectList) 378 ObjectList->push_back(std::move(Json)); 379 else 380 printJSON(std::move(Json)); 381 return false; 382 } 383 384 void JSONPrinter::listBegin() { 385 assert(!ObjectList); 386 ObjectList = std::make_unique<json::Array>(); 387 } 388 389 void JSONPrinter::listEnd() { 390 assert(ObjectList); 391 printJSON(std::move(*ObjectList)); 392 ObjectList.reset(); 393 } 394 395 } // end namespace symbolize 396 } // end namespace llvm 397