Trace.cpp
2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//===-- Trace.cpp ---------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "lldb/Target/Trace.h"
#include <sstream>
#include "llvm/Support/Format.h"
#include "lldb/Core/PluginManager.h"
using namespace lldb;
using namespace lldb_private;
using namespace llvm;
// Helper structs used to extract the type of a trace settings json without
// having to parse the entire object.
struct JSONSimplePluginSettings {
std::string type;
};
struct JSONSimpleTraceSettings {
JSONSimplePluginSettings trace;
};
namespace llvm {
namespace json {
bool fromJSON(const json::Value &value,
JSONSimplePluginSettings &plugin_settings, json::Path path) {
json::ObjectMapper o(value, path);
return o && o.map("type", plugin_settings.type);
}
bool fromJSON(const json::Value &value, JSONSimpleTraceSettings &settings,
json::Path path) {
json::ObjectMapper o(value, path);
return o && o.map("trace", settings.trace);
}
} // namespace json
} // namespace llvm
llvm::Expected<lldb::TraceSP> Trace::FindPlugin(Debugger &debugger,
const json::Value &settings,
StringRef info_dir) {
JSONSimpleTraceSettings json_settings;
json::Path::Root root("settings");
if (!json::fromJSON(settings, json_settings, root))
return root.getError();
ConstString plugin_name(json_settings.trace.type);
auto create_callback = PluginManager::GetTraceCreateCallback(plugin_name);
if (create_callback) {
TraceSP instance = create_callback();
if (llvm::Error err = instance->ParseSettings(debugger, settings, info_dir))
return std::move(err);
return instance;
}
return createStringError(
std::errc::invalid_argument,
"no trace plug-in matches the specified type: \"%s\"",
plugin_name.AsCString());
}
llvm::Expected<lldb::TraceSP> Trace::FindPlugin(StringRef name) {
ConstString plugin_name(name);
auto create_callback = PluginManager::GetTraceCreateCallback(plugin_name);
if (create_callback)
return create_callback();
return createStringError(
std::errc::invalid_argument,
"no trace plug-in matches the specified type: \"%s\"",
plugin_name.AsCString());
}
llvm::Error Trace::ParseSettings(Debugger &debugger,
const llvm::json::Value &settings,
llvm::StringRef settings_dir) {
if (llvm::Error err =
CreateParser()->ParseSettings(debugger, settings, settings_dir))
return err;
return llvm::Error::success();
}
llvm::StringRef Trace::GetSchema() { return CreateParser()->GetSchema(); }