• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "tooling/base/pt_events.h"
17 
18 namespace panda::ecmascript::tooling {
ToJson() const19 std::unique_ptr<PtJson> BreakpointResolved::ToJson() const
20 {
21     std::unique_ptr<PtJson> result = PtJson::CreateObject();
22     result->Add("breakpointId", breakpointId_.c_str());
23     ASSERT(location_ != nullptr);
24     result->Add("location", location_->ToJson());
25 
26     std::unique_ptr<PtJson> object = PtJson::CreateObject();
27     object->Add("method", GetName().c_str());
28     object->Add("params", result);
29 
30     return object;
31 }
32 
ToJson() const33 std::unique_ptr<PtJson> Paused::ToJson() const
34 {
35     std::unique_ptr<PtJson> result = PtJson::CreateObject();
36 
37     std::unique_ptr<PtJson> array = PtJson::CreateArray();
38     size_t len = callFrames_.size();
39     for (size_t i = 0; i < len; i++) {
40         ASSERT(callFrames_[i] != nullptr);
41         array->Push(callFrames_[i]->ToJson());
42     }
43     result->Add("callFrames", array);
44     result->Add("reason", reason_.c_str());
45     if (data_) {
46         ASSERT(data_.value() != nullptr);
47         result->Add("data", data_.value()->ToJson());
48     }
49     if (hitBreakpoints_) {
50         std::unique_ptr<PtJson> breakpoints = PtJson::CreateArray();
51         len = hitBreakpoints_->size();
52         for (size_t i = 0; i < len; i++) {
53             breakpoints->Push(hitBreakpoints_.value()[i].c_str());
54         }
55         result->Add("hitBreakpoints", breakpoints);
56     }
57 
58     std::unique_ptr<PtJson> object = PtJson::CreateObject();
59     object->Add("method", GetName().c_str());
60     object->Add("params", result);
61 
62     return object;
63 }
64 
ToJson() const65 std::unique_ptr<PtJson> Resumed::ToJson() const
66 {
67     std::unique_ptr<PtJson> result = PtJson::CreateObject();
68 
69     std::unique_ptr<PtJson> object = PtJson::CreateObject();
70     object->Add("method", GetName().c_str());
71     object->Add("params", result);
72 
73     return object;
74 }
75 
ToJson() const76 std::unique_ptr<PtJson> NativeCalling::ToJson() const
77 {
78     std::unique_ptr<PtJson> result = PtJson::CreateObject();
79 
80     result->Add("nativeAddress", reinterpret_cast<int64_t>(GetNativeAddress()));
81     result->Add("isStepInto", GetIntoStatus());
82 
83     std::unique_ptr<PtJson> object = PtJson::CreateObject();
84     object->Add("method", GetName().c_str());
85     object->Add("params", result);
86 
87     return object;
88 }
89 
ToJson() const90 std::unique_ptr<PtJson> MixedStack::ToJson() const
91 {
92     std::unique_ptr<PtJson> result = PtJson::CreateObject();
93 
94     std::unique_ptr<PtJson> nativePointerArray = PtJson::CreateArray();
95     size_t nativePointerLength = nativePointer_.size();
96     for (size_t i = 0; i < nativePointerLength; ++i) {
97         nativePointerArray->Push(reinterpret_cast<int64_t>(nativePointer_[i]));
98     }
99     result->Add("nativePointer", nativePointerArray);
100 
101     std::unique_ptr<PtJson> callFrameArray = PtJson::CreateArray();
102     size_t callFrameLength = callFrames_.size();
103     for (size_t i = 0; i < callFrameLength; ++i) {
104         ASSERT(callFrames_[i] != nullptr);
105         callFrameArray->Push(callFrames_[i]->ToJson());
106     }
107     result->Add("callFrames", callFrameArray);
108 
109     std::unique_ptr<PtJson> object = PtJson::CreateObject();
110     object->Add("method", GetName().c_str());
111     object->Add("params", result);
112 
113     return object;
114 }
115 
ToJson() const116 std::unique_ptr<PtJson> ScriptFailedToParse::ToJson() const
117 {
118     std::unique_ptr<PtJson> result = PtJson::CreateObject();
119 
120     result->Add("scriptId", std::to_string(scriptId_).c_str());
121     result->Add("url", url_.c_str());
122     result->Add("startLine", startLine_);
123     result->Add("startColumn", startColumn_);
124     result->Add("endLine", endLine_);
125     result->Add("endColumn", endColumn_);
126     result->Add("executionContextId", executionContextId_);
127     result->Add("hash", hash_.c_str());
128     if (sourceMapUrl_) {
129         result->Add("sourceMapURL", sourceMapUrl_->c_str());
130     }
131     if (hasSourceUrl_) {
132         result->Add("hasSourceURL", hasSourceUrl_.value());
133     }
134     if (isModule_) {
135         result->Add("isModule", isModule_.value());
136     }
137     if (length_) {
138         result->Add("length", length_.value());
139     }
140     if (codeOffset_) {
141         result->Add("codeOffset", codeOffset_.value());
142     }
143     if (scriptLanguage_) {
144         result->Add("scriptLanguage", scriptLanguage_->c_str());
145     }
146     if (embedderName_) {
147         result->Add("embedderName", embedderName_->c_str());
148     }
149 
150     std::unique_ptr<PtJson> object = PtJson::CreateObject();
151     object->Add("method", GetName().c_str());
152     object->Add("params", result);
153 
154     return object;
155 }
156 
ToJson() const157 std::unique_ptr<PtJson> ScriptParsed::ToJson() const
158 {
159     std::unique_ptr<PtJson> result = PtJson::CreateObject();
160 
161     result->Add("scriptId", std::to_string(scriptId_).c_str());
162     result->Add("url", url_.c_str());
163     result->Add("startLine", startLine_);
164     result->Add("startColumn", startColumn_);
165     result->Add("endLine", endLine_);
166     result->Add("endColumn", endColumn_);
167     result->Add("executionContextId", executionContextId_);
168     result->Add("hash", hash_.c_str());
169     if (isLiveEdit_) {
170         result->Add("isLiveEdit", isLiveEdit_.value());
171     }
172     if (sourceMapUrl_) {
173         result->Add("sourceMapURL", sourceMapUrl_->c_str());
174     }
175     if (hasSourceUrl_) {
176         result->Add("hasSourceURL", hasSourceUrl_.value());
177     }
178     if (isModule_) {
179         result->Add("isModule", isModule_.value());
180     }
181     if (length_) {
182         result->Add("length", length_.value());
183     }
184     if (codeOffset_) {
185         result->Add("codeOffset", codeOffset_.value());
186     }
187     if (scriptLanguage_) {
188         result->Add("scriptLanguage", scriptLanguage_->c_str());
189     }
190     if (embedderName_) {
191         result->Add("embedderName", embedderName_->c_str());
192     }
193 
194     std::unique_ptr<PtJson> array = PtJson::CreateArray();
195     size_t len = locations_.size();
196     for (size_t i = 0; i < len; i++) {
197         ASSERT(locations_[i] != nullptr);
198         std::unique_ptr<PtJson> location = locations_[i]->ToJson();
199         array->Push(location);
200     }
201     result->Add("locations", array);
202 
203     std::unique_ptr<PtJson> object = PtJson::CreateObject();
204     object->Add("method", GetName().c_str());
205     object->Add("params", result);
206 
207     return object;
208 }
209 
ToJson() const210 std::unique_ptr<PtJson> AddHeapSnapshotChunk::ToJson() const
211 {
212     std::unique_ptr<PtJson> result = PtJson::CreateObject();
213 
214     result->Add("chunk", chunk_.c_str());
215 
216     std::unique_ptr<PtJson> object = PtJson::CreateObject();
217     object->Add("method", GetName().c_str());
218     object->Add("params", result);
219 
220     return object;
221 }
222 
ToJson() const223 std::unique_ptr<PtJson> ConsoleProfileFinished::ToJson() const
224 {
225     std::unique_ptr<PtJson> result = PtJson::CreateObject();
226 
227     result->Add("id", id_.c_str());
228     ASSERT(location_ != nullptr);
229     result->Add("location", location_->ToJson());
230     ASSERT(profile_ != nullptr);
231     result->Add("profile", profile_->ToJson());
232     if (title_) {
233         result->Add("title", title_->c_str());
234     }
235 
236     std::unique_ptr<PtJson> object = PtJson::CreateObject();
237     object->Add("method", GetName().c_str());
238     object->Add("params", result);
239 
240     return object;
241 }
242 
ToJson() const243 std::unique_ptr<PtJson> ConsoleProfileStarted::ToJson() const
244 {
245     std::unique_ptr<PtJson> result = PtJson::CreateObject();
246 
247     result->Add("id", id_.c_str());
248     ASSERT(location_ != nullptr);
249     result->Add("location", location_->ToJson());
250     if (title_) {
251         result->Add("title", title_->c_str());
252     }
253 
254     std::unique_ptr<PtJson> object = PtJson::CreateObject();
255     object->Add("method", GetName().c_str());
256     object->Add("params", result);
257 
258     return object;
259 }
260 
ToJson() const261 std::unique_ptr<PtJson> PreciseCoverageDeltaUpdate::ToJson() const
262 {
263     std::unique_ptr<PtJson> result = PtJson::CreateObject();
264 
265     result->Add("timestamp", timestamp_);
266     result->Add("occasion", occasion_.c_str());
267     std::unique_ptr<PtJson> array = PtJson::CreateArray();
268     size_t len = result_.size();
269     for (size_t i = 0; i < len; i++) {
270         ASSERT(result_[i] != nullptr);
271         std::unique_ptr<PtJson> res = result_[i]->ToJson();
272         array->Push(res);
273     }
274     result->Add("result", array);
275 
276     std::unique_ptr<PtJson> object = PtJson::CreateObject();
277     object->Add("method", GetName().c_str());
278     object->Add("params", result);
279 
280     return object;
281 }
282 
ToJson() const283 std::unique_ptr<PtJson> HeapStatsUpdate::ToJson() const
284 {
285     std::unique_ptr<PtJson> result = PtJson::CreateObject();
286 
287     std::unique_ptr<PtJson> array = PtJson::CreateArray();
288     size_t len = statsUpdate_.size();
289     for (size_t i = 0; i < len; i++) {
290         array->Push(statsUpdate_[i]);
291     }
292     result->Add("statsUpdate", array);
293 
294     std::unique_ptr<PtJson> object = PtJson::CreateObject();
295     object->Add("method", GetName().c_str());
296     object->Add("params", result);
297 
298     return object;
299 }
300 
ToJson() const301 std::unique_ptr<PtJson> LastSeenObjectId::ToJson() const
302 {
303     std::unique_ptr<PtJson> result = PtJson::CreateObject();
304 
305     result->Add("lastSeenObjectId", lastSeenObjectId_);
306     result->Add("timestamp", timestamp_);
307 
308     std::unique_ptr<PtJson> object = PtJson::CreateObject();
309     object->Add("method", GetName().c_str());
310     object->Add("params", result);
311 
312     return object;
313 }
314 
ToJson() const315 std::unique_ptr<PtJson> ReportHeapSnapshotProgress::ToJson() const
316 {
317     std::unique_ptr<PtJson> result = PtJson::CreateObject();
318 
319     result->Add("done", done_);
320     result->Add("total", total_);
321     if (finished_) {
322         result->Add("finished", finished_.value());
323     }
324 
325     std::unique_ptr<PtJson> object = PtJson::CreateObject();
326     object->Add("method", GetName().c_str());
327     object->Add("params", result);
328 
329     return object;
330 }
331 
ToJson() const332 std::unique_ptr<PtJson> BufferUsage::ToJson() const
333 {
334     std::unique_ptr<PtJson> result = PtJson::CreateObject();
335 
336     if (percentFull_) {
337         result->Add("percentFull", percentFull_.value());
338     }
339     if (eventCount_) {
340         result->Add("eventCount", eventCount_.value());
341     }
342     if (value_) {
343         result->Add("value", value_.value());
344     }
345 
346     std::unique_ptr<PtJson> object = PtJson::CreateObject();
347     object->Add("method", GetName().c_str());
348     object->Add("params", result);
349 
350     return object;
351 }
352 
TraceEventToJson(TraceEvent & traceEvent) const353 std::unique_ptr<PtJson> DataCollected::TraceEventToJson(TraceEvent &traceEvent) const
354 {
355     std::unique_ptr<PtJson> event = PtJson::CreateObject();
356     std::unique_ptr<PtJson> args = PtJson::Parse(traceEvent.args_);
357     event->Add("args", args);
358     event->Add("cat", traceEvent.cat_.c_str());
359 
360     if (traceEvent.dur_ != 0) {
361         event->Add("dur", traceEvent.dur_);
362     }
363 
364     if (!traceEvent.id_.empty()) {
365         event->Add("id", traceEvent.id_.c_str());
366     }
367 
368     event->Add("name", traceEvent.name_.c_str());
369     event->Add("ph", traceEvent.ph_.c_str());
370     event->Add("pid", traceEvent.pid_);
371 
372     if (!traceEvent.s_.empty()) {
373         event->Add("s", traceEvent.s_.c_str());
374     }
375 
376     if (traceEvent.tdur_ != 0) {
377         event->Add("tdur", traceEvent.tdur_);
378     }
379 
380     event->Add("tid", traceEvent.tid_);
381     event->Add("ts", traceEvent.ts_);
382 
383     if (traceEvent.tts_ != 0) {
384         event->Add("tts", traceEvent.tts_);
385     }
386 
387     return event;
388 }
389 
ToJson() const390 std::unique_ptr<PtJson> DataCollected::ToJson() const
391 {
392     std::unique_ptr<PtJson> traceEvents = PtJson::CreateArray();
393     for (auto &traceEvent : *traceEvents_) {
394         traceEvents->Push(TraceEventToJson(traceEvent));
395     }
396 
397     std::unique_ptr<PtJson> value = PtJson::CreateObject();
398     value->Add("value", traceEvents);
399 
400     std::unique_ptr<PtJson> object = PtJson::CreateObject();
401     object->Add("method", GetName().c_str());
402     object->Add("params", value);
403 
404     return object;
405 }
406 
ToJson() const407 std::unique_ptr<PtJson> TracingComplete::ToJson() const
408 {
409     std::unique_ptr<PtJson> result = PtJson::CreateObject();
410 
411     result->Add("dataLossOccurred", dataLossOccurred_);
412     if (traceFormat_) {
413         result->Add("traceFormat", traceFormat_.value()->c_str());
414     }
415     if (streamCompression_) {
416         result->Add("streamCompression", streamCompression_.value()->c_str());
417     }
418 
419     std::unique_ptr<PtJson> object = PtJson::CreateObject();
420     object->Add("method", GetName().c_str());
421     object->Add("params", result);
422 
423     return object;
424 }
425 }  // namespace panda::ecmascript::tooling
426