xref: /freebsd-src/contrib/llvm-project/lldb/source/Plugins/Trace/intel-pt/TraceIntelPTBundleLoader.h (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
181ad6265SDimitry Andric //===-- TraceIntelPTBundleLoader.h ----------------------------*- C++ //-*-===//
281ad6265SDimitry Andric //
381ad6265SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
481ad6265SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
581ad6265SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
681ad6265SDimitry Andric //
781ad6265SDimitry Andric //===----------------------------------------------------------------------===//
881ad6265SDimitry Andric 
981ad6265SDimitry Andric #ifndef LLDB_SOURCE_PLUGINS_TRACE_INTEL_PT_TRACEINTELPTBUNDLELOADER_H
1081ad6265SDimitry Andric #define LLDB_SOURCE_PLUGINS_TRACE_INTEL_PT_TRACEINTELPTBUNDLELOADER_H
1181ad6265SDimitry Andric 
1281ad6265SDimitry Andric #include "../common/ThreadPostMortemTrace.h"
1381ad6265SDimitry Andric #include "TraceIntelPTJSONStructs.h"
1481ad6265SDimitry Andric 
1581ad6265SDimitry Andric namespace lldb_private {
1681ad6265SDimitry Andric namespace trace_intel_pt {
1781ad6265SDimitry Andric 
1881ad6265SDimitry Andric class TraceIntelPT;
1981ad6265SDimitry Andric 
2081ad6265SDimitry Andric class TraceIntelPTBundleLoader {
2181ad6265SDimitry Andric public:
2281ad6265SDimitry Andric   /// Helper struct holding the objects created when parsing a process
2381ad6265SDimitry Andric   struct ParsedProcess {
2481ad6265SDimitry Andric     lldb::TargetSP target_sp;
2581ad6265SDimitry Andric     std::vector<lldb::ThreadPostMortemTraceSP> threads;
2681ad6265SDimitry Andric   };
2781ad6265SDimitry Andric 
2881ad6265SDimitry Andric   /// \param[in] debugger
2981ad6265SDimitry Andric   ///   The debugger that will own the targets to create.
3081ad6265SDimitry Andric   ///
3181ad6265SDimitry Andric   /// \param[in] bundle_description
32*bdd1243dSDimitry Andric   ///   The JSON description of a trace bundle that follows the schema of the
33*bdd1243dSDimitry Andric   ///   intel pt trace plug-in.
3481ad6265SDimitry Andric   ///
3581ad6265SDimitry Andric   /// \param[in] bundle_dir
3681ad6265SDimitry Andric   ///   The folder where the trace bundle is located.
TraceIntelPTBundleLoader(Debugger & debugger,const llvm::json::Value & bundle_description,llvm::StringRef bundle_dir)3781ad6265SDimitry Andric   TraceIntelPTBundleLoader(Debugger &debugger,
3881ad6265SDimitry Andric                            const llvm::json::Value &bundle_description,
3981ad6265SDimitry Andric                            llvm::StringRef bundle_dir)
4081ad6265SDimitry Andric       : m_debugger(debugger), m_bundle_description(bundle_description),
4181ad6265SDimitry Andric         m_bundle_dir(bundle_dir) {}
4281ad6265SDimitry Andric 
4381ad6265SDimitry Andric   /// \return
4481ad6265SDimitry Andric   ///   The JSON schema for the bundle description.
4581ad6265SDimitry Andric   static llvm::StringRef GetSchema();
4681ad6265SDimitry Andric 
4781ad6265SDimitry Andric   /// Parse the trace bundle description and create the corresponding \a
4881ad6265SDimitry Andric   /// Target objects. In case of an error, no targets are created.
4981ad6265SDimitry Andric   ///
5081ad6265SDimitry Andric   /// \return
51*bdd1243dSDimitry Andric   ///   A \a lldb::TraceSP instance created according to the trace bundle
52*bdd1243dSDimitry Andric   ///   information. In case of errors, return a null pointer.
5381ad6265SDimitry Andric   llvm::Expected<lldb::TraceSP> Load();
5481ad6265SDimitry Andric 
5581ad6265SDimitry Andric private:
5681ad6265SDimitry Andric   /// Resolve non-absolute paths relative to the bundle folder.
5781ad6265SDimitry Andric   FileSpec NormalizePath(const std::string &path);
5881ad6265SDimitry Andric 
5981ad6265SDimitry Andric   /// Create a post-mortem thread associated with the given \p process
6081ad6265SDimitry Andric   /// using the definition from \p thread.
6181ad6265SDimitry Andric   lldb::ThreadPostMortemTraceSP ParseThread(Process &process,
6281ad6265SDimitry Andric                                             const JSONThread &thread);
6381ad6265SDimitry Andric 
6481ad6265SDimitry Andric   /// Given a bundle description and a list of fully parsed processes,
6581ad6265SDimitry Andric   /// create an actual Trace instance that "traces" these processes.
6681ad6265SDimitry Andric   llvm::Expected<lldb::TraceSP>
6781ad6265SDimitry Andric   CreateTraceIntelPTInstance(JSONTraceBundleDescription &bundle_description,
6881ad6265SDimitry Andric                              std::vector<ParsedProcess> &parsed_processes);
6981ad6265SDimitry Andric 
70*bdd1243dSDimitry Andric   /// Create an empty Process object with given pid and target.
71*bdd1243dSDimitry Andric   llvm::Expected<ParsedProcess> CreateEmptyProcess(lldb::pid_t pid,
72*bdd1243dSDimitry Andric                                                    llvm::StringRef triple);
73*bdd1243dSDimitry Andric 
7481ad6265SDimitry Andric   /// Create the corresponding Threads and Process objects given the JSON
7581ad6265SDimitry Andric   /// process definition.
7681ad6265SDimitry Andric   ///
7781ad6265SDimitry Andric   /// \param[in] process
7881ad6265SDimitry Andric   ///   The JSON process definition
7981ad6265SDimitry Andric   llvm::Expected<ParsedProcess> ParseProcess(const JSONProcess &process);
8081ad6265SDimitry Andric 
81*bdd1243dSDimitry Andric   /// Create a module associated with the given \p target using the definition
82*bdd1243dSDimitry Andric   /// from \p module.
8381ad6265SDimitry Andric   llvm::Error ParseModule(Target &target, const JSONModule &module);
8481ad6265SDimitry Andric 
85*bdd1243dSDimitry Andric   /// Create a kernel process and cpu threads given the JSON kernel definition.
86*bdd1243dSDimitry Andric   llvm::Expected<ParsedProcess>
87*bdd1243dSDimitry Andric   ParseKernel(const JSONTraceBundleDescription &bundle_description);
88*bdd1243dSDimitry Andric 
8981ad6265SDimitry Andric   /// Create a user-friendly error message upon a JSON-parsing failure using the
9081ad6265SDimitry Andric   /// \a json::ObjectMapper functionality.
9181ad6265SDimitry Andric   ///
9281ad6265SDimitry Andric   /// \param[in] root
9381ad6265SDimitry Andric   ///   The \a llvm::json::Path::Root used to parse the JSON \a value.
9481ad6265SDimitry Andric   ///
9581ad6265SDimitry Andric   /// \param[in] value
9681ad6265SDimitry Andric   ///   The json value that failed to parse.
9781ad6265SDimitry Andric   ///
9881ad6265SDimitry Andric   /// \return
9981ad6265SDimitry Andric   ///   An \a llvm::Error containing the user-friendly error message.
10081ad6265SDimitry Andric   llvm::Error CreateJSONError(llvm::json::Path::Root &root,
10181ad6265SDimitry Andric                               const llvm::json::Value &value);
10281ad6265SDimitry Andric 
10381ad6265SDimitry Andric   /// Create the corresponding Process, Thread and Module objects given this
10481ad6265SDimitry Andric   /// bundle description.
10581ad6265SDimitry Andric   llvm::Expected<std::vector<ParsedProcess>>
10681ad6265SDimitry Andric   LoadBundle(const JSONTraceBundleDescription &bundle_description);
10781ad6265SDimitry Andric 
10881ad6265SDimitry Andric   /// When applicable, augment the list of threads in the trace bundle by
10981ad6265SDimitry Andric   /// inspecting the context switch trace. This only applies for threads of
11081ad6265SDimitry Andric   /// processes already specified in this bundle description.
11181ad6265SDimitry Andric   ///
11281ad6265SDimitry Andric   /// \return
11381ad6265SDimitry Andric   ///   An \a llvm::Error in case if failures, or \a llvm::Error::success
11481ad6265SDimitry Andric   ///   otherwise.
115*bdd1243dSDimitry Andric   llvm::Error AugmentThreadsFromContextSwitches(
116*bdd1243dSDimitry Andric       JSONTraceBundleDescription &bundle_description);
11781ad6265SDimitry Andric 
118*bdd1243dSDimitry Andric   /// Modifiy the bundle description by normalizing all the paths relative to
119*bdd1243dSDimitry Andric   /// the session file directory.
12081ad6265SDimitry Andric   void NormalizeAllPaths(JSONTraceBundleDescription &bundle_description);
12181ad6265SDimitry Andric 
12281ad6265SDimitry Andric   Debugger &m_debugger;
12381ad6265SDimitry Andric   const llvm::json::Value &m_bundle_description;
12481ad6265SDimitry Andric   const std::string m_bundle_dir;
12581ad6265SDimitry Andric };
12681ad6265SDimitry Andric 
12781ad6265SDimitry Andric } // namespace trace_intel_pt
12881ad6265SDimitry Andric } // namespace lldb_private
12981ad6265SDimitry Andric 
13081ad6265SDimitry Andric #endif // LLDB_SOURCE_PLUGINS_TRACE_INTEL_PT_TRACEINTELPTBUNDLELOADER_H
131