DIPrinter.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/DebugInfo/Symbolize/DIPrinter.h ---------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file declares the DIPrinter class, which is responsible for printing
  15. // structures defined in DebugInfo/DIContext.h
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_DEBUGINFO_SYMBOLIZE_DIPRINTER_H
  19. #define LLVM_DEBUGINFO_SYMBOLIZE_DIPRINTER_H
  20. #include "llvm/ADT/Optional.h"
  21. #include "llvm/ADT/StringRef.h"
  22. #include "llvm/Support/JSON.h"
  23. #include <memory>
  24. #include <vector>
  25. namespace llvm {
  26. struct DILineInfo;
  27. class DIInliningInfo;
  28. struct DIGlobal;
  29. struct DILocal;
  30. class ErrorInfoBase;
  31. class raw_ostream;
  32. namespace symbolize {
  33. class SourceCode;
  34. struct Request {
  35. StringRef ModuleName;
  36. Optional<uint64_t> Address;
  37. };
  38. class DIPrinter {
  39. public:
  40. DIPrinter() = default;
  41. virtual ~DIPrinter() = default;
  42. virtual void print(const Request &Request, const DILineInfo &Info) = 0;
  43. virtual void print(const Request &Request, const DIInliningInfo &Info) = 0;
  44. virtual void print(const Request &Request, const DIGlobal &Global) = 0;
  45. virtual void print(const Request &Request,
  46. const std::vector<DILocal> &Locals) = 0;
  47. virtual void printInvalidCommand(const Request &Request,
  48. StringRef Command) = 0;
  49. virtual bool printError(const Request &Request,
  50. const ErrorInfoBase &ErrorInfo,
  51. StringRef ErrorBanner) = 0;
  52. virtual void listBegin() = 0;
  53. virtual void listEnd() = 0;
  54. };
  55. struct PrinterConfig {
  56. bool PrintAddress;
  57. bool PrintFunctions;
  58. bool Pretty;
  59. bool Verbose;
  60. int SourceContextLines;
  61. };
  62. class PlainPrinterBase : public DIPrinter {
  63. protected:
  64. raw_ostream &OS;
  65. raw_ostream &ES;
  66. PrinterConfig Config;
  67. void print(const DILineInfo &Info, bool Inlined);
  68. void printFunctionName(StringRef FunctionName, bool Inlined);
  69. virtual void printSimpleLocation(StringRef Filename,
  70. const DILineInfo &Info) = 0;
  71. void printContext(SourceCode SourceCode);
  72. void printVerbose(StringRef Filename, const DILineInfo &Info);
  73. virtual void printStartAddress(const DILineInfo &Info) {}
  74. virtual void printFooter() {}
  75. private:
  76. void printHeader(uint64_t Address);
  77. public:
  78. PlainPrinterBase(raw_ostream &OS, raw_ostream &ES, PrinterConfig &Config)
  79. : OS(OS), ES(ES), Config(Config) {}
  80. void print(const Request &Request, const DILineInfo &Info) override;
  81. void print(const Request &Request, const DIInliningInfo &Info) override;
  82. void print(const Request &Request, const DIGlobal &Global) override;
  83. void print(const Request &Request,
  84. const std::vector<DILocal> &Locals) override;
  85. void printInvalidCommand(const Request &Request, StringRef Command) override;
  86. bool printError(const Request &Request, const ErrorInfoBase &ErrorInfo,
  87. StringRef ErrorBanner) override;
  88. void listBegin() override {}
  89. void listEnd() override {}
  90. };
  91. class LLVMPrinter : public PlainPrinterBase {
  92. private:
  93. void printSimpleLocation(StringRef Filename, const DILineInfo &Info) override;
  94. void printStartAddress(const DILineInfo &Info) override;
  95. void printFooter() override;
  96. public:
  97. LLVMPrinter(raw_ostream &OS, raw_ostream &ES, PrinterConfig &Config)
  98. : PlainPrinterBase(OS, ES, Config) {}
  99. };
  100. class GNUPrinter : public PlainPrinterBase {
  101. private:
  102. void printSimpleLocation(StringRef Filename, const DILineInfo &Info) override;
  103. public:
  104. GNUPrinter(raw_ostream &OS, raw_ostream &ES, PrinterConfig &Config)
  105. : PlainPrinterBase(OS, ES, Config) {}
  106. };
  107. class JSONPrinter : public DIPrinter {
  108. private:
  109. raw_ostream &OS;
  110. PrinterConfig Config;
  111. std::unique_ptr<json::Array> ObjectList;
  112. void printJSON(const json::Value &V) {
  113. json::OStream JOS(OS, Config.Pretty ? 2 : 0);
  114. JOS.value(V);
  115. OS << '\n';
  116. }
  117. public:
  118. JSONPrinter(raw_ostream &OS, PrinterConfig &Config)
  119. : OS(OS), Config(Config) {}
  120. void print(const Request &Request, const DILineInfo &Info) override;
  121. void print(const Request &Request, const DIInliningInfo &Info) override;
  122. void print(const Request &Request, const DIGlobal &Global) override;
  123. void print(const Request &Request,
  124. const std::vector<DILocal> &Locals) override;
  125. void printInvalidCommand(const Request &Request, StringRef Command) override;
  126. bool printError(const Request &Request, const ErrorInfoBase &ErrorInfo,
  127. StringRef ErrorBanner) override;
  128. void listBegin() override;
  129. void listEnd() override;
  130. };
  131. } // namespace symbolize
  132. } // namespace llvm
  133. #endif
  134. #ifdef __GNUC__
  135. #pragma GCC diagnostic pop
  136. #endif