1 //===-- Trace.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 #include "lldb/Target/Trace.h"
10 
11 #include <sstream>
12 
13 #include "llvm/Support/Format.h"
14 
15 #include "lldb/Core/PluginManager.h"
16 
17 using namespace lldb;
18 using namespace lldb_private;
19 using namespace llvm;
20 
21 // Helper structs used to extract the type of a trace session json without
22 // having to parse the entire object.
23 
24 struct JSONSimplePluginSettings {
25   std::string type;
26 };
27 
28 struct JSONSimpleTraceSession {
29   JSONSimplePluginSettings trace;
30 };
31 
32 namespace llvm {
33 namespace json {
34 
35 bool fromJSON(const Value &value, JSONSimplePluginSettings &plugin_settings,
36               Path path) {
37   json::ObjectMapper o(value, path);
38   return o && o.map("type", plugin_settings.type);
39 }
40 
41 bool fromJSON(const Value &value, JSONSimpleTraceSession &session, Path path) {
42   json::ObjectMapper o(value, path);
43   return o && o.map("trace", session.trace);
44 }
45 
46 } // namespace json
47 } // namespace llvm
48 
49 static Error createInvalidPlugInError(StringRef plugin_name) {
50   return createStringError(
51       std::errc::invalid_argument,
52       "no trace plug-in matches the specified type: \"%s\"",
53       plugin_name.data());
54 }
55 
56 Expected<lldb::TraceSP> Trace::FindPlugin(Debugger &debugger,
57                                           const json::Value &trace_session_file,
58                                           StringRef session_file_dir) {
59   JSONSimpleTraceSession json_session;
60   json::Path::Root root("traceSession");
61   if (!json::fromJSON(trace_session_file, json_session, root))
62     return root.getError();
63 
64   ConstString plugin_name(json_session.trace.type);
65   if (auto create_callback = PluginManager::GetTraceCreateCallback(plugin_name))
66     return create_callback(trace_session_file, session_file_dir, debugger);
67 
68   return createInvalidPlugInError(json_session.trace.type);
69 }
70 
71 Expected<StringRef> Trace::FindPluginSchema(StringRef name) {
72   ConstString plugin_name(name);
73   StringRef schema = PluginManager::GetTraceSchema(plugin_name);
74   if (!schema.empty())
75     return schema;
76 
77   return createInvalidPlugInError(name);
78 }
79