• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 "ecmascript/dfx/stackinfo/async_stack_trace.h"
17 
18 #include "ecmascript/dfx/stackinfo/js_stackinfo.h"
19 #include "ecmascript/js_promise.h"
20 #include "ecmascript/ecma_vm.h"
21 #include "ecmascript/platform/async_detect.h"
22 
23 namespace panda::ecmascript {
AsyncStackTrace(EcmaVM * vm)24 AsyncStackTrace::AsyncStackTrace(EcmaVM *vm) : vm_(vm)
25 {
26     jsThread_ = vm->GetJSThread();
27 }
28 
RegisterAsyncDetectCallBack()29 void AsyncStackTrace::RegisterAsyncDetectCallBack()
30 {
31     panda::ecmascript::RegisterAsyncDetectCallBack(vm_);
32 }
33 
InsertAsyncStackTrace(const JSHandle<JSPromise> & promise)34 bool AsyncStackTrace::InsertAsyncStackTrace(const JSHandle<JSPromise> &promise)
35 {
36     std::string stack = JsStackInfo::BuildJsStackTrace(jsThread_, false, false);
37     if (stack.empty()) {
38         return false;
39     }
40     auto now = std::chrono::system_clock::now();
41     auto currentTime = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count();
42     asyncStackTrace_.emplace(static_cast<uint32_t>(promise->GetAsyncTaskId()), std::make_pair(stack, currentTime));
43     return true;
44 }
45 
RemoveAsyncStackTrace(const JSHandle<JSPromise> & promise)46 bool AsyncStackTrace::RemoveAsyncStackTrace(const JSHandle<JSPromise> &promise)
47 {
48     auto asyncStackTrace = asyncStackTrace_.find(static_cast<uint32_t>(promise->GetAsyncTaskId()));
49     if (asyncStackTrace == asyncStackTrace_.end()) {
50         LOG_ECMA(INFO) << "Not find promise: " << static_cast<uint32_t>(promise->GetAsyncTaskId());
51         return false;
52     }
53     asyncStackTrace_.erase(asyncStackTrace);
54     return true;
55 }
56 }  // namespace panda::ecmascript