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/share_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     AINT_D,
33     BUILTIN,
34     NAPI,
35     ARKUI_ENGINE,
36     RUNTIME,
37     JIT
38 };
39 
40 struct MethodKey {
41     void *methodIdentifier = nullptr;
42     RunningState state = RunningState::OTHER;
43     kungfu::DeoptType deoptType = kungfu::DeoptType::NONE;
44     int lineNumber = 0;
operator <panda::ecmascript::MethodKey45     bool operator < (const MethodKey &methodKey) const
46     {
47         return state < methodKey.state ||
48                (state == methodKey.state && methodIdentifier < methodKey.methodIdentifier) ||
49                (state == methodKey.state && methodIdentifier == methodKey.methodIdentifier &&
50                deoptType < methodKey.deoptType) ||
51                (state == methodKey.state && methodIdentifier == methodKey.methodIdentifier &&
52                deoptType == methodKey.deoptType && lineNumber < methodKey.lineNumber);
53     }
54 };
55 
56 struct NodeKey {
57     struct MethodKey methodKey = {0};
58     int parentId = 0;
operator <panda::ecmascript::NodeKey59     bool operator < (const NodeKey &nodeKey) const
60     {
61         return parentId < nodeKey.parentId ||
62                (parentId == nodeKey.parentId && methodKey < nodeKey.methodKey);
63     }
64 };
65 
66 struct FrameInfoTemp {
67     char codeType[20] = {0}; // 20:the maximum size of the codeType
68     char functionName[100] = {0}; // 100:the maximum size of the functionName
69     char recordName[500] = {0}; // 500:the maximum size of the recordName
70     int columnNumber = -1;
71     int lineNumber = -1;
72     int scriptId = 0;
73     char url[500] = {0}; // 500:the maximum size of the url
74     struct MethodKey methodKey = {0};
75 };
76 
77 class JsStackGetter {
78 public:
79     static bool CheckFrameType(JSThread *thread, JSTaggedType *sp);
80     static bool ParseMethodInfo(struct MethodKey &methodKey,
81                                 const FrameIterator &it,
82                                 const EcmaVM *vm,
83                                 FrameInfoTemp &codeEntry,
84                                 bool isCpuProfiler = false);
85     static bool CheckAndCopy(char *dest, size_t length, const char *src);
86     static void GetNativeStack(const EcmaVM *vm, const FrameIterator &it, char *functionName,
87                                size_t size, bool isCpuProfiler);
88     static RunningState GetRunningState(const FrameIterator &it, const EcmaVM *vm, bool isNative,
89                                         bool topFrame, bool enableVMTag = false);
90     static void GetNativeMethodCallPos(FrameIterator &it, FrameInfoTemp &codeEntry);
91     static void *GetMethodIdentifier(Method *method, const FrameIterator &it);
92     static void GetCallLineNumber(const FrameIterator &it, int &LineNumber);
93 };
94 } // namespace panda::ecmascript
95 #endif  // ECMASCRIPT_DFX_STACKINFO_JS_STACKGETTER_H
96