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/dynamic/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 if (asyncStack_ && asyncCallChainDepth_) {
59 result->Add("asyncStackTrace", ToJson(*asyncStack_, asyncCallChainDepth_ - 1));
60 }
61
62 std::unique_ptr<PtJson> object = PtJson::CreateObject();
63 object->Add("method", GetName().c_str());
64 object->Add("params", result);
65 return object;
66 }
67
ToJson(StackFrame stackFrame) const68 std::unique_ptr<PtJson> Paused::ToJson(StackFrame stackFrame) const
69 {
70 std::unique_ptr<PtJson> result = PtJson::CreateObject();
71
72 std::string functionName = stackFrame.GetFunctionName();
73 std::string url = stackFrame.GetUrl();
74 int32_t scriptId = stackFrame.GetScriptId();
75 int32_t lineNumber = stackFrame.GetLineNumber();
76 int32_t columnNumber = stackFrame.GetColumnNumber();
77 result->Add("functionName", functionName.c_str());
78 result->Add("scriptId", scriptId);
79 result->Add("url", url.c_str());
80 result->Add("lineNumber", lineNumber);
81 result->Add("columnNumber", columnNumber);
82
83 return result;
84 }
85
ToJson(AsyncStack asyncStack,int32_t asyncCallChainDepth) const86 std::unique_ptr<PtJson> Paused::ToJson(AsyncStack asyncStack, int32_t asyncCallChainDepth) const
87 {
88 std::unique_ptr<PtJson> result = PtJson::CreateObject();
89
90 std::unique_ptr<PtJson> array = PtJson::CreateArray();
91 std::vector<std::shared_ptr<StackFrame>> callFrames = asyncStack.GetFrames();
92 size_t len = callFrames.size();
93 for (size_t i = 0; i < len; i++) {
94 array->Push(ToJson(*callFrames[i]));
95 }
96 result->Add("callFrames", array);
97 std::string description = asyncStack.GetDescription();
98 result->Add("description", description.c_str());
99
100 std::weak_ptr<AsyncStack> weakAsyncStack = asyncStack.GetAsyncParent();
101 auto sharedAsyncStack = weakAsyncStack.lock();
102 if (sharedAsyncStack && asyncCallChainDepth) {
103 asyncCallChainDepth--;
104 result->Add("parent", ToJson(*sharedAsyncStack, asyncCallChainDepth));
105 }
106
107 return result;
108 }
109
ToJson() const110 std::unique_ptr<PtJson> Resumed::ToJson() const
111 {
112 std::unique_ptr<PtJson> result = PtJson::CreateObject();
113
114 std::unique_ptr<PtJson> object = PtJson::CreateObject();
115 object->Add("method", GetName().c_str());
116 object->Add("params", result);
117
118 return object;
119 }
120
ToJson() const121 std::unique_ptr<PtJson> NativeCalling::ToJson() const
122 {
123 std::unique_ptr<PtJson> result = PtJson::CreateObject();
124
125 result->Add("nativeAddress", reinterpret_cast<int64_t>(GetNativeAddress()));
126 result->Add("isStepInto", GetIntoStatus());
127
128 std::unique_ptr<PtJson> object = PtJson::CreateObject();
129 object->Add("method", GetName().c_str());
130 object->Add("params", result);
131
132 return object;
133 }
134
ToJson() const135 std::unique_ptr<PtJson> MixedStack::ToJson() const
136 {
137 std::unique_ptr<PtJson> result = PtJson::CreateObject();
138
139 std::unique_ptr<PtJson> nativePointerArray = PtJson::CreateArray();
140 size_t nativePointerLength = nativePointer_.size();
141 for (size_t i = 0; i < nativePointerLength; ++i) {
142 nativePointerArray->Push(reinterpret_cast<int64_t>(nativePointer_[i]));
143 }
144 result->Add("nativePointer", nativePointerArray);
145
146 std::unique_ptr<PtJson> callFrameArray = PtJson::CreateArray();
147 size_t callFrameLength = callFrames_.size();
148 for (size_t i = 0; i < callFrameLength; ++i) {
149 ASSERT(callFrames_[i] != nullptr);
150 callFrameArray->Push(callFrames_[i]->ToJson());
151 }
152 result->Add("callFrames", callFrameArray);
153
154 std::unique_ptr<PtJson> object = PtJson::CreateObject();
155 object->Add("method", GetName().c_str());
156 object->Add("params", result);
157
158 return object;
159 }
160
ToJson() const161 std::unique_ptr<PtJson> ScriptFailedToParse::ToJson() const
162 {
163 std::unique_ptr<PtJson> result = PtJson::CreateObject();
164
165 result->Add("scriptId", std::to_string(scriptId_).c_str());
166 result->Add("url", url_.c_str());
167 result->Add("startLine", startLine_);
168 result->Add("startColumn", startColumn_);
169 result->Add("endLine", endLine_);
170 result->Add("endColumn", endColumn_);
171 result->Add("executionContextId", executionContextId_);
172 result->Add("hash", hash_.c_str());
173 if (sourceMapUrl_) {
174 result->Add("sourceMapURL", sourceMapUrl_->c_str());
175 }
176 if (hasSourceUrl_) {
177 result->Add("hasSourceURL", hasSourceUrl_.value());
178 }
179 if (isModule_) {
180 result->Add("isModule", isModule_.value());
181 }
182 if (length_) {
183 result->Add("length", length_.value());
184 }
185 if (codeOffset_) {
186 result->Add("codeOffset", codeOffset_.value());
187 }
188 if (scriptLanguage_) {
189 result->Add("scriptLanguage", scriptLanguage_->c_str());
190 }
191 if (embedderName_) {
192 result->Add("embedderName", embedderName_->c_str());
193 }
194
195 std::unique_ptr<PtJson> object = PtJson::CreateObject();
196 object->Add("method", GetName().c_str());
197 object->Add("params", result);
198
199 return object;
200 }
201
ToJson() const202 std::unique_ptr<PtJson> ScriptParsed::ToJson() const
203 {
204 std::unique_ptr<PtJson> result = PtJson::CreateObject();
205
206 result->Add("scriptId", std::to_string(scriptId_).c_str());
207 result->Add("url", url_.c_str());
208 result->Add("startLine", startLine_);
209 result->Add("startColumn", startColumn_);
210 result->Add("endLine", endLine_);
211 result->Add("endColumn", endColumn_);
212 result->Add("executionContextId", executionContextId_);
213 result->Add("hash", hash_.c_str());
214 if (isLiveEdit_) {
215 result->Add("isLiveEdit", isLiveEdit_.value());
216 }
217 if (sourceMapUrl_) {
218 result->Add("sourceMapURL", sourceMapUrl_->c_str());
219 }
220 if (hasSourceUrl_) {
221 result->Add("hasSourceURL", hasSourceUrl_.value());
222 }
223 if (isModule_) {
224 result->Add("isModule", isModule_.value());
225 }
226 if (length_) {
227 result->Add("length", length_.value());
228 }
229 if (codeOffset_) {
230 result->Add("codeOffset", codeOffset_.value());
231 }
232 if (scriptLanguage_) {
233 result->Add("scriptLanguage", scriptLanguage_->c_str());
234 }
235 if (embedderName_) {
236 result->Add("embedderName", embedderName_->c_str());
237 }
238
239 std::unique_ptr<PtJson> array = PtJson::CreateArray();
240 size_t len = locations_.size();
241 for (size_t i = 0; i < len; i++) {
242 ASSERT(locations_[i] != nullptr);
243 std::unique_ptr<PtJson> location = locations_[i]->ToJson();
244 array->Push(location);
245 }
246 result->Add("locations", array);
247
248 std::unique_ptr<PtJson> object = PtJson::CreateObject();
249 object->Add("method", GetName().c_str());
250 object->Add("params", result);
251
252 return object;
253 }
254
ToJson() const255 std::unique_ptr<PtJson> AddHeapSnapshotChunk::ToJson() const
256 {
257 std::unique_ptr<PtJson> result = PtJson::CreateObject();
258
259 result->Add("chunk", chunk_.c_str());
260
261 std::unique_ptr<PtJson> object = PtJson::CreateObject();
262 object->Add("method", GetName().c_str());
263 object->Add("params", result);
264
265 return object;
266 }
267
ToJson() const268 std::unique_ptr<PtJson> ConsoleProfileFinished::ToJson() const
269 {
270 std::unique_ptr<PtJson> result = PtJson::CreateObject();
271
272 result->Add("id", id_.c_str());
273 ASSERT(location_ != nullptr);
274 result->Add("location", location_->ToJson());
275 ASSERT(profile_ != nullptr);
276 result->Add("profile", profile_->ToJson());
277 if (title_) {
278 result->Add("title", title_->c_str());
279 }
280
281 std::unique_ptr<PtJson> object = PtJson::CreateObject();
282 object->Add("method", GetName().c_str());
283 object->Add("params", result);
284
285 return object;
286 }
287
ToJson() const288 std::unique_ptr<PtJson> ConsoleProfileStarted::ToJson() const
289 {
290 std::unique_ptr<PtJson> result = PtJson::CreateObject();
291
292 result->Add("id", id_.c_str());
293 ASSERT(location_ != nullptr);
294 result->Add("location", location_->ToJson());
295 if (title_) {
296 result->Add("title", title_->c_str());
297 }
298
299 std::unique_ptr<PtJson> object = PtJson::CreateObject();
300 object->Add("method", GetName().c_str());
301 object->Add("params", result);
302
303 return object;
304 }
305
ToJson() const306 std::unique_ptr<PtJson> PreciseCoverageDeltaUpdate::ToJson() const
307 {
308 std::unique_ptr<PtJson> result = PtJson::CreateObject();
309
310 result->Add("timestamp", timestamp_);
311 result->Add("occasion", occasion_.c_str());
312 std::unique_ptr<PtJson> array = PtJson::CreateArray();
313 size_t len = result_.size();
314 for (size_t i = 0; i < len; i++) {
315 ASSERT(result_[i] != nullptr);
316 std::unique_ptr<PtJson> res = result_[i]->ToJson();
317 array->Push(res);
318 }
319 result->Add("result", array);
320
321 std::unique_ptr<PtJson> object = PtJson::CreateObject();
322 object->Add("method", GetName().c_str());
323 object->Add("params", result);
324
325 return object;
326 }
327
ToJson() const328 std::unique_ptr<PtJson> HeapStatsUpdate::ToJson() const
329 {
330 std::unique_ptr<PtJson> result = PtJson::CreateObject();
331
332 std::unique_ptr<PtJson> array = PtJson::CreateArray();
333 size_t len = statsUpdate_.size();
334 for (size_t i = 0; i < len; i++) {
335 array->Push(statsUpdate_[i]);
336 }
337 result->Add("statsUpdate", array);
338
339 std::unique_ptr<PtJson> object = PtJson::CreateObject();
340 object->Add("method", GetName().c_str());
341 object->Add("params", result);
342
343 return object;
344 }
345
ToJson() const346 std::unique_ptr<PtJson> LastSeenObjectId::ToJson() const
347 {
348 std::unique_ptr<PtJson> result = PtJson::CreateObject();
349
350 result->Add("lastSeenObjectId", lastSeenObjectId_);
351 result->Add("timestamp", timestamp_);
352
353 std::unique_ptr<PtJson> object = PtJson::CreateObject();
354 object->Add("method", GetName().c_str());
355 object->Add("params", result);
356
357 return object;
358 }
359
ToJson() const360 std::unique_ptr<PtJson> ReportHeapSnapshotProgress::ToJson() const
361 {
362 std::unique_ptr<PtJson> result = PtJson::CreateObject();
363
364 result->Add("done", done_);
365 result->Add("total", total_);
366 if (finished_) {
367 result->Add("finished", finished_.value());
368 }
369
370 std::unique_ptr<PtJson> object = PtJson::CreateObject();
371 object->Add("method", GetName().c_str());
372 object->Add("params", result);
373
374 return object;
375 }
376
ToJson() const377 std::unique_ptr<PtJson> BufferUsage::ToJson() const
378 {
379 std::unique_ptr<PtJson> result = PtJson::CreateObject();
380
381 if (percentFull_) {
382 result->Add("percentFull", percentFull_.value());
383 }
384 if (eventCount_) {
385 result->Add("eventCount", eventCount_.value());
386 }
387 if (value_) {
388 result->Add("value", value_.value());
389 }
390
391 std::unique_ptr<PtJson> object = PtJson::CreateObject();
392 object->Add("method", GetName().c_str());
393 object->Add("params", result);
394
395 return object;
396 }
397
TraceEventToJson(TraceEvent & traceEvent) const398 std::unique_ptr<PtJson> DataCollected::TraceEventToJson(TraceEvent &traceEvent) const
399 {
400 std::unique_ptr<PtJson> event = PtJson::CreateObject();
401 std::unique_ptr<PtJson> args = PtJson::Parse(traceEvent.args_);
402 event->Add("args", args);
403 event->Add("cat", traceEvent.cat_.c_str());
404
405 if (traceEvent.dur_ != 0) {
406 event->Add("dur", traceEvent.dur_);
407 }
408
409 if (!traceEvent.id_.empty()) {
410 event->Add("id", traceEvent.id_.c_str());
411 }
412
413 event->Add("name", traceEvent.name_.c_str());
414 event->Add("ph", traceEvent.ph_.c_str());
415 event->Add("pid", traceEvent.pid_);
416
417 if (!traceEvent.s_.empty()) {
418 event->Add("s", traceEvent.s_.c_str());
419 }
420
421 if (traceEvent.tdur_ != 0) {
422 event->Add("tdur", traceEvent.tdur_);
423 }
424
425 event->Add("tid", traceEvent.tid_);
426 event->Add("ts", traceEvent.ts_);
427
428 if (traceEvent.tts_ != 0) {
429 event->Add("tts", traceEvent.tts_);
430 }
431
432 return event;
433 }
434
ToJson() const435 std::unique_ptr<PtJson> DataCollected::ToJson() const
436 {
437 std::unique_ptr<PtJson> traceEvents = PtJson::CreateArray();
438 for (auto &traceEvent : *traceEvents_) {
439 traceEvents->Push(TraceEventToJson(traceEvent));
440 }
441
442 std::unique_ptr<PtJson> value = PtJson::CreateObject();
443 value->Add("value", traceEvents);
444
445 std::unique_ptr<PtJson> object = PtJson::CreateObject();
446 object->Add("method", GetName().c_str());
447 object->Add("params", value);
448
449 return object;
450 }
451
ToJson() const452 std::unique_ptr<PtJson> TracingComplete::ToJson() const
453 {
454 std::unique_ptr<PtJson> result = PtJson::CreateObject();
455
456 result->Add("dataLossOccurred", dataLossOccurred_);
457 if (traceFormat_) {
458 result->Add("traceFormat", traceFormat_.value()->c_str());
459 }
460 if (streamCompression_) {
461 result->Add("streamCompression", streamCompression_.value()->c_str());
462 }
463
464 std::unique_ptr<PtJson> object = PtJson::CreateObject();
465 object->Add("method", GetName().c_str());
466 object->Add("params", result);
467
468 return object;
469 }
470 } // namespace panda::ecmascript::tooling
471