1 //===-- TraceGDBRemotePackets.cpp -------------------------------*- 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 #include "lldb/Utility/TraceGDBRemotePackets.h"
10 
11 using namespace llvm;
12 using namespace llvm::json;
13 
14 namespace lldb_private {
15 /// jLLDBTraceSupported
16 /// \{
17 bool fromJSON(const json::Value &value, TraceSupportedResponse &packet,
18               Path path) {
19   ObjectMapper o(value, path);
20   return o && o.map("description", packet.description) &&
21          o.map("name", packet.name);
22 }
23 
24 json::Value toJSON(const TraceSupportedResponse &packet) {
25   return json::Value(
26       Object{{"description", packet.description}, {"name", packet.name}});
27 }
28 /// \}
29 
30 /// jLLDBTraceStart
31 /// \{
32 bool TraceStartRequest::IsProcessTracing() const { return !(bool)tids; }
33 
34 bool fromJSON(const json::Value &value, TraceStartRequest &packet, Path path) {
35   ObjectMapper o(value, path);
36   return o && o.map("type", packet.type) && o.map("tids", packet.tids);
37 }
38 
39 json::Value toJSON(const TraceStartRequest &packet) {
40   return json::Value(Object{{"tids", packet.tids}, {"type", packet.type}});
41 }
42 /// \}
43 
44 /// jLLDBTraceStop
45 /// \{
46 TraceStopRequest::TraceStopRequest(llvm::StringRef type,
47                                    const std::vector<lldb::tid_t> &tids_)
48     : type(type) {
49   tids.emplace();
50   for (lldb::tid_t tid : tids_)
51     tids->push_back(tid);
52 }
53 
54 bool TraceStopRequest::IsProcessTracing() const { return !(bool)tids; }
55 
56 bool fromJSON(const json::Value &value, TraceStopRequest &packet, Path path) {
57   ObjectMapper o(value, path);
58   return o && o.map("type", packet.type) && o.map("tids", packet.tids);
59 }
60 
61 json::Value toJSON(const TraceStopRequest &packet) {
62   return json::Value(Object{{"type", packet.type}, {"tids", packet.tids}});
63 }
64 /// \}
65 
66 /// jLLDBTraceGetState
67 /// \{
68 bool fromJSON(const json::Value &value, TraceGetStateRequest &packet,
69               Path path) {
70   ObjectMapper o(value, path);
71   return o && o.map("type", packet.type);
72 }
73 
74 json::Value toJSON(const TraceGetStateRequest &packet) {
75   return json::Value(Object{{"type", packet.type}});
76 }
77 
78 bool fromJSON(const json::Value &value, TraceBinaryData &packet, Path path) {
79   ObjectMapper o(value, path);
80   return o && o.map("kind", packet.kind) && o.map("size", packet.size);
81 }
82 
83 json::Value toJSON(const TraceBinaryData &packet) {
84   return json::Value(Object{{"kind", packet.kind}, {"size", packet.size}});
85 }
86 
87 bool fromJSON(const json::Value &value, TraceThreadState &packet, Path path) {
88   ObjectMapper o(value, path);
89   return o && o.map("tid", packet.tid) &&
90          o.map("binaryData", packet.binary_data);
91 }
92 
93 json::Value toJSON(const TraceThreadState &packet) {
94   return json::Value(
95       Object{{"tid", packet.tid}, {"binaryData", packet.binary_data}});
96 }
97 
98 bool fromJSON(const json::Value &value, TraceGetStateResponse &packet,
99               Path path) {
100   ObjectMapper o(value, path);
101   return o && o.map("tracedThreads", packet.traced_threads) &&
102          o.map("processBinaryData", packet.process_binary_data) &&
103          o.map("cores", packet.cores) &&
104          o.map("warnings", packet.warnings);
105 }
106 
107 json::Value toJSON(const TraceGetStateResponse &packet) {
108   return json::Value(Object{{"tracedThreads", packet.traced_threads},
109                             {"processBinaryData", packet.process_binary_data},
110                             {"cores", packet.cores},
111                             {"warnings", packet.warnings}});
112 }
113 
114 void TraceGetStateResponse::AddWarning(StringRef warning) {
115   if (!warnings)
116     warnings.emplace();
117   warnings->push_back(warning.data());
118 }
119 
120 bool fromJSON(const json::Value &value, TraceCoreState &packet,
121               json::Path path) {
122   ObjectMapper o(value, path);
123   int64_t core_id;
124   if (!o || !o.map("coreId", core_id) ||
125       !o.map("binaryData", packet.binary_data))
126     return false;
127   packet.core_id = static_cast<lldb::core_id_t>(core_id);
128   return true;
129 }
130 
131 json::Value toJSON(const TraceCoreState &packet) {
132   return json::Value(
133       Object{{"coreId", packet.core_id}, {"binaryData", packet.binary_data}});
134 }
135 /// \}
136 
137 /// jLLDBTraceGetBinaryData
138 /// \{
139 json::Value toJSON(const TraceGetBinaryDataRequest &packet) {
140   return json::Value(Object{{"type", packet.type},
141                             {"kind", packet.kind},
142                             {"offset", packet.offset},
143                             {"tid", packet.tid},
144                             {"coreId", packet.core_id},
145                             {"size", packet.size}});
146 }
147 
148 bool fromJSON(const json::Value &value, TraceGetBinaryDataRequest &packet,
149               Path path) {
150   ObjectMapper o(value, path);
151   return o && o.map("type", packet.type) && o.map("kind", packet.kind) &&
152          o.map("tid", packet.tid) && o.map("offset", packet.offset) &&
153          o.map("size", packet.size) && o.map("coreId", packet.core_id);
154 }
155 /// \}
156 
157 } // namespace lldb_private
158