1 /* 2 * Copyright (c) 2023 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_DFX_STACKINFO_JS_STACKGETTER_H 17 #define ECMASCRIPT_DFX_STACKINFO_JS_STACKGETTER_H 18 19 #include <csignal> 20 21 #include "ecmascript/compiler/gate_meta_data.h" 22 #include "ecmascript/deoptimizer/deoptimizer.h" 23 #include "ecmascript/interpreter/frame_handler.h" 24 25 namespace panda::ecmascript { 26 enum class RunningState : size_t { 27 OTHER = 0, 28 GC, 29 CINT, 30 AINT, 31 AOT, 32 BUILTIN, 33 NAPI, 34 ARKUI_ENGINE, 35 RUNTIME 36 }; 37 38 struct MethodKey { 39 void *methodIdentifier = nullptr; 40 RunningState state = RunningState::OTHER; 41 kungfu::DeoptType deoptType = kungfu::DeoptType::NOTCHECK; 42 bool operator < (const MethodKey &methodKey) const 43 { 44 return state < methodKey.state || 45 (state == methodKey.state && methodIdentifier < methodKey.methodIdentifier) || 46 (state == methodKey.state && methodIdentifier == methodKey.methodIdentifier && 47 deoptType < methodKey.deoptType); 48 } 49 }; 50 51 struct NodeKey { 52 struct MethodKey methodKey = {0}; 53 int parentId = 0; 54 bool operator < (const NodeKey &nodeKey) const 55 { 56 return parentId < nodeKey.parentId || 57 (parentId == nodeKey.parentId && methodKey < nodeKey.methodKey); 58 } 59 }; 60 61 struct FrameInfoTemp { 62 char codeType[20] = {0}; // 20:the maximum size of the codeType 63 char functionName[100] = {0}; // 100:the maximum size of the functionName 64 char recordName[500] = {0}; // 500:the maximum size of the recordName 65 int columnNumber = -1; 66 int lineNumber = -1; 67 int scriptId = 0; 68 char url[500] = {0}; // 500:the maximum size of the url 69 struct MethodKey methodKey = {0}; 70 }; 71 72 class JsStackGetter { 73 public: 74 static bool CheckFrameType(JSThread *thread, JSTaggedType *sp); 75 static bool ParseMethodInfo(struct MethodKey &methodKey, 76 const FrameIterator &it, 77 const EcmaVM *vm, 78 FrameInfoTemp &codeEntry, 79 bool isCpuProfiler = false); 80 static bool CheckAndCopy(char *dest, size_t length, const char *src); 81 static void GetNativeStack(const EcmaVM *vm, const FrameIterator &it, char *functionName, 82 size_t size, bool isCpuProfiler); 83 static RunningState GetRunningState(const FrameIterator &it, const EcmaVM *vm, bool isNative, 84 bool topFrame, bool enableVMTag = false); 85 static void GetNativeMethodCallPos(FrameIterator &it, FrameInfoTemp &codeEntry); 86 static void *GetMethodIdentifier(Method *method, const FrameIterator &it); 87 }; 88 } // namespace panda::ecmascript 89 #endif // ECMASCRIPT_DFX_STACKINFO_JS_STACKGETTER_H