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 
9 #ifndef LLDB_TARGET_TRACESESSIONPARSER_H
10 #define LLDB_TARGET_TRACESESSIONPARSER_H
11 
12 #include "ThreadPostMortemTrace.h"
13 #include "TraceJSONStructs.h"
14 
15 namespace lldb_private {
16 
17 /// \class TraceSessionFileParser TraceSessionFileParser.h
18 ///
19 /// Base class for parsing the common information of JSON trace session files.
20 /// Contains the basic C++ structs that represent the JSON data, which include
21 /// \a JSONTraceSession as the root object.
22 ///
23 /// See \a Trace::FindPlugin for more information regarding these JSON files.
24 class TraceSessionFileParser {
25 public:
26 
27   /// Helper struct holding the objects created when parsing a process
28   struct ParsedProcess {
29     lldb::TargetSP target_sp;
30     std::vector<lldb::ThreadPostMortemTraceSP> threads;
31   };
32 
TraceSessionFileParser(Debugger & debugger,llvm::StringRef session_file_dir,llvm::StringRef schema)33   TraceSessionFileParser(Debugger &debugger, llvm::StringRef session_file_dir,
34                          llvm::StringRef schema)
35       : m_debugger(debugger), m_session_file_dir(session_file_dir),
36         m_schema(schema) {}
37 
38   /// Build the full schema for a Trace plug-in.
39   ///
40   /// \param[in] plugin_schema
41   ///   The subschema that corresponds to the "trace" section of the schema.
42   ///
43   /// \return
44   ///   The full schema containing the common attributes and the plug-in
45   ///   specific attributes.
46   static std::string BuildSchema(llvm::StringRef plugin_schema);
47 
48   /// Parse the fields common to all trace session schemas.
49   ///
50   /// \param[in] session
51   ///     The session json objects already deserialized.
52   ///
53   /// \return
54   ///     A list of \a ParsedProcess containing all threads and targets created
55   ///     during the parsing, or an error in case of failures. In case of
56   ///     errors, no side effects are produced.
57   llvm::Expected<std::vector<ParsedProcess>>
58   ParseCommonSessionFile(const JSONTraceSessionBase &session);
59 
60 protected:
61   /// Resolve non-absolute paths relative to the session file folder. It
62   /// modifies the given file_spec.
63   void NormalizePath(lldb_private::FileSpec &file_spec);
64 
65   lldb::ThreadPostMortemTraceSP ParseThread(lldb::ProcessSP &process_sp,
66                                             const JSONThread &thread);
67 
68   llvm::Expected<ParsedProcess> ParseProcess(const JSONProcess &process);
69 
70   llvm::Error ParseModule(lldb::TargetSP &target_sp, const JSONModule &module);
71 
72   /// Create a user-friendly error message upon a JSON-parsing failure using the
73   /// \a json::ObjectMapper functionality.
74   ///
75   /// \param[in] root
76   ///   The \a llvm::json::Path::Root used to parse the JSON \a value.
77   ///
78   /// \param[in] value
79   ///   The json value that failed to parse.
80   ///
81   /// \return
82   ///   An \a llvm::Error containing the user-friendly error message.
83   llvm::Error CreateJSONError(llvm::json::Path::Root &root,
84                               const llvm::json::Value &value);
85 
86   Debugger &m_debugger;
87   std::string m_session_file_dir;
88   llvm::StringRef m_schema;
89 };
90 } // namespace lldb_private
91 
92 
93 #endif // LLDB_TARGET_TRACESESSIONPARSER_H
94