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 #ifndef ECMASCRIPT_STACKINFO_STACK_TRACE_H 17 #define ECMASCRIPT_STACKINFO_STACK_TRACE_H 18 19 #include <string> 20 #include "ecmascript/mem/c_containers.h" 21 #include "ecmascript/dfx/stackinfo/js_stackinfo.h" 22 23 namespace panda::ecmascript { 24 class JSPromise; 25 class AsyncStackTrace { 26 public: 27 explicit AsyncStackTrace(EcmaVM *vm); 28 29 ~AsyncStackTrace() = default; 30 31 void RegisterAsyncDetectCallBack(); 32 33 bool InsertAsyncStackTrace(const JSHandle<JSPromise> &promise); 34 35 bool RemoveAsyncStackTrace(const JSHandle<JSPromise> &promise); 36 GetAsyncStackTrace()37 CMap<uint32_t, std::pair<std::string, int64_t>> GetAsyncStackTrace() 38 { 39 return asyncStackTrace_; 40 } 41 GetAsyncTaskId()42 uint32_t GetAsyncTaskId() 43 { 44 if (asyncTaskId_ == UINT32_MAX) { 45 asyncTaskId_ = 0; 46 } 47 return ++asyncTaskId_; 48 } 49 50 static constexpr uint32_t PROMISE_PENDING_TIME_MS = 5000; 51 52 NO_COPY_SEMANTIC(AsyncStackTrace); 53 NO_MOVE_SEMANTIC(AsyncStackTrace); 54 private: 55 EcmaVM *vm_ {nullptr}; 56 JSThread *jsThread_ {nullptr}; 57 uint32_t asyncTaskId_ {0}; 58 59 // { promiseid , (jsStack, time) } 60 CMap<uint32_t, std::pair<std::string, int64_t>> asyncStackTrace_; 61 }; 62 } // namespace panda::ecmascript 63 #endif // ECMASCRIPT_STACKINFO_STACK_TRACE_H