TraceSessionFileParser.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. //===-- TraceSessionFileParser.h --------------------------------*- C++ -*-===//
  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. #ifndef LLDB_TARGET_TRACESESSIONPARSER_H
  9. #define LLDB_TARGET_TRACESESSIONPARSER_H
  10. #include "llvm/Support/JSON.h"
  11. #include "lldb/Target/ThreadPostMortemTrace.h"
  12. namespace lldb_private {
  13. /// \class TraceSessionFileParser TraceSessionFileParser.h
  14. ///
  15. /// Base class for parsing the common information of JSON trace session files.
  16. /// Contains the basic C++ structs that represent the JSON data, which include
  17. /// \a JSONTraceSession as the root object.
  18. ///
  19. /// See \a Trace::FindPlugin for more information regarding these JSON files.
  20. class TraceSessionFileParser {
  21. public:
  22. /// C++ structs representing the JSON trace session.
  23. /// \{
  24. struct JSONAddress {
  25. lldb::addr_t value;
  26. };
  27. struct JSONModule {
  28. std::string system_path;
  29. llvm::Optional<std::string> file;
  30. JSONAddress load_address;
  31. llvm::Optional<std::string> uuid;
  32. };
  33. struct JSONThread {
  34. int64_t tid;
  35. std::string trace_file;
  36. };
  37. struct JSONProcess {
  38. int64_t pid;
  39. std::string triple;
  40. std::vector<JSONThread> threads;
  41. std::vector<JSONModule> modules;
  42. };
  43. struct JSONTracePluginSettings {
  44. std::string type;
  45. };
  46. struct JSONTraceSessionBase {
  47. std::vector<JSONProcess> processes;
  48. };
  49. /// The trace plug-in implementation should provide its own TPluginSettings,
  50. /// which corresponds to the "trace" section of the schema.
  51. template <class TPluginSettings>
  52. struct JSONTraceSession : JSONTraceSessionBase {
  53. TPluginSettings trace;
  54. };
  55. /// \}
  56. /// Helper struct holding the objects created when parsing a process
  57. struct ParsedProcess {
  58. lldb::TargetSP target_sp;
  59. std::vector<lldb::ThreadPostMortemTraceSP> threads;
  60. };
  61. TraceSessionFileParser(Debugger &debugger, llvm::StringRef session_file_dir,
  62. llvm::StringRef schema)
  63. : m_debugger(debugger), m_session_file_dir(session_file_dir),
  64. m_schema(schema) {}
  65. /// Build the full schema for a Trace plug-in.
  66. ///
  67. /// \param[in] plugin_schema
  68. /// The subschema that corresponds to the "trace" section of the schema.
  69. ///
  70. /// \return
  71. /// The full schema containing the common attributes and the plug-in
  72. /// specific attributes.
  73. static std::string BuildSchema(llvm::StringRef plugin_schema);
  74. /// Parse the fields common to all trace session schemas.
  75. ///
  76. /// \param[in] session
  77. /// The session json objects already deserialized.
  78. ///
  79. /// \return
  80. /// A list of \a ParsedProcess containing all threads and targets created
  81. /// during the parsing, or an error in case of failures. In case of
  82. /// errors, no side effects are produced.
  83. llvm::Expected<std::vector<ParsedProcess>>
  84. ParseCommonSessionFile(const JSONTraceSessionBase &session);
  85. protected:
  86. /// Resolve non-absolute paths relative to the session file folder. It
  87. /// modifies the given file_spec.
  88. void NormalizePath(lldb_private::FileSpec &file_spec);
  89. lldb::ThreadPostMortemTraceSP ParseThread(lldb::ProcessSP &process_sp,
  90. const JSONThread &thread);
  91. llvm::Expected<ParsedProcess> ParseProcess(const JSONProcess &process);
  92. llvm::Error ParseModule(lldb::TargetSP &target_sp, const JSONModule &module);
  93. /// Create a user-friendly error message upon a JSON-parsing failure using the
  94. /// \a json::ObjectMapper functionality.
  95. ///
  96. /// \param[in] root
  97. /// The \a llvm::json::Path::Root used to parse the JSON \a value.
  98. ///
  99. /// \param[in] value
  100. /// The json value that failed to parse.
  101. ///
  102. /// \return
  103. /// An \a llvm::Error containing the user-friendly error message.
  104. llvm::Error CreateJSONError(llvm::json::Path::Root &root,
  105. const llvm::json::Value &value);
  106. Debugger &m_debugger;
  107. std::string m_session_file_dir;
  108. llvm::StringRef m_schema;
  109. };
  110. } // namespace lldb_private
  111. namespace llvm {
  112. namespace json {
  113. bool fromJSON(const Value &value,
  114. lldb_private::TraceSessionFileParser::JSONAddress &address,
  115. Path path);
  116. bool fromJSON(const Value &value,
  117. lldb_private::TraceSessionFileParser::JSONModule &module,
  118. Path path);
  119. bool fromJSON(const Value &value,
  120. lldb_private::TraceSessionFileParser::JSONThread &thread,
  121. Path path);
  122. bool fromJSON(const Value &value,
  123. lldb_private::TraceSessionFileParser::JSONProcess &process,
  124. Path path);
  125. bool fromJSON(const Value &value,
  126. lldb_private::TraceSessionFileParser::JSONTracePluginSettings
  127. &plugin_settings,
  128. Path path);
  129. bool fromJSON(
  130. const Value &value,
  131. lldb_private::TraceSessionFileParser::JSONTraceSessionBase &session,
  132. Path path);
  133. template <class TPluginSettings>
  134. bool fromJSON(
  135. const Value &value,
  136. lldb_private::TraceSessionFileParser::JSONTraceSession<TPluginSettings>
  137. &session,
  138. Path path) {
  139. ObjectMapper o(value, path);
  140. return o && o.map("trace", session.trace) &&
  141. fromJSON(value,
  142. (lldb_private::TraceSessionFileParser::JSONTraceSessionBase &)
  143. session,
  144. path);
  145. }
  146. } // namespace json
  147. } // namespace llvm
  148. #endif // LLDB_TARGET_TRACESESSIONPARSER_H