DIPrinter.cpp 12 KB

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