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 DFX_EXCEPTION_H
17 #define DFX_EXCEPTION_H
18
19 #include <inttypes.h>
20 #include "dfx_define.h"
21 #include "dfx_socket_request.h"
22
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26
27 enum CrashExceptionCode : int32_t {
28 CRASH_ESUCCESS = 0, /* No error */
29
30 CRASH_SIGNAL_EMASKED = 101, /* Signal has been masked */
31 CRASH_SIGNAL_EFORK, /* Failed to fork child process */
32 CRASH_SIGNAL_ECLONE, /* Failed to clone thread of recycle dump process */
33 CRASH_SIGNAL_ESETSTATE, /* Failed to set dump state */
34 CRASH_SIGNAL_EINHERITCAP, /* Failed to inherit capabilities */
35 CRASH_SIGNAL_EEXECL, /* Failed to execl processdump */
36 CRASH_SIGNAL_EWAITEXIT, /* Failed to wait vm process exit */
37 CRASH_SIGNAL_EREADPIPE, /* Failed to read pipe due to timeout */
38 CRASH_SIGNAL_ECREATEPIPE, /* Failed to init create pipe */
39 CRASH_SIGNAL_EDUMPREQUEST, /* Failed to find symbol to dump request */
40 CRASH_SIGNAL_EWAITPIDTIMEOUT, /* Signal handler waitpid timeout */
41
42 CRASH_DUMP_EREADREQ = 201, /* Failed to read dump request */
43 CRASH_DUMP_EPARENTPID, /* Failed to check parent pid */
44 CRASH_DUMP_EATTACH, /* Failed to attach target process */
45 CRASH_DUMP_EWRITEFD, /* Failed to request writen fd */
46 CRASH_DUMP_EKILLED, /* Tagert process has been killed */
47 CRASH_DUMP_LOCAL_REPORT, /* Local Handler DumpInfo Report*/
48 CRASH_DUMP_EREADPID, /* Failed to read real pid*/
49
50 CRASH_UNWIND_ECONTEXT = 301, /* Unwind context illegal */
51 CRASH_UNWIND_EFRAME, /* Failed to step ark js frame */
52 CRASH_UNWIND_ESTACK, /* Stack corruption */
53
54 CRASH_LOG_ESTACKLOS = 401, /* Crash thread stack not found */
55 CRASH_LOG_ECHILDSTACK, /* Child thread stack not found */
56 CRASH_LOG_EREGLOS, /* Registers not found */
57 CRASH_LOG_EMEMLOS, /* Memory not found */
58 CRASH_LOG_ESTACKMEMLOS, /* Fault stack not found */
59 CRASH_LOG_EMAPLOS, /* Maps not found */
60 CRASH_LOG_EHILOGLOS, /* Hilog not found */
61 CRASH_LOG_ESUMMARYLOS, /* Fault Summary not found */
62 CRASH_LOG_EPROCESS_LIFECYCLE, /* Process lifecycle is null */
63
64 CRASH_UNKNOWN = 500, /* Unknown reason */
65 };
66
67 struct ErrCodeToMsg {
68 /** Crash exception stage code */
69 int32_t errCode;
70 /** Crash exception msg string */
71 const char* msg;
72 };
73
GetCrashDescription(int32_t errCode)74 static inline const char* GetCrashDescription(int32_t errCode)
75 {
76 const struct ErrCodeToMsg crashExceptions[] = {
77 { CRASH_SIGNAL_EMASKED, "Signal has been masked." },
78 { CRASH_SIGNAL_EFORK, "Failed to fork child process." },
79 { CRASH_SIGNAL_ECLONE, "Failed to clone thread of recycle dump process." },
80 { CRASH_SIGNAL_ESETSTATE, "Failed to set dump state." },
81 { CRASH_SIGNAL_EINHERITCAP, "Failed to inherit capabilities." },
82 { CRASH_SIGNAL_EEXECL, "Failed to execl processdump." },
83 { CRASH_SIGNAL_EWAITEXIT, "Failed to wait vm process exit." },
84 { CRASH_SIGNAL_EREADPIPE, "Failed to read pipe due to timeout."},
85 { CRASH_SIGNAL_ECREATEPIPE, "Failed to init create pipe."},
86 { CRASH_SIGNAL_EDUMPREQUEST, "Failed to find symbol to dump request."},
87 { CRASH_SIGNAL_EWAITPIDTIMEOUT, "Signal handler waitpid timeout"},
88 { CRASH_DUMP_EREADREQ, "Failed to read dump request." },
89 { CRASH_DUMP_EPARENTPID, "Failed to check parent pid." },
90 { CRASH_DUMP_EATTACH, "Failed to attach target process." },
91 { CRASH_DUMP_EWRITEFD, "Failed to request writen fd." },
92 { CRASH_DUMP_EKILLED, "Tagert process has been killed." },
93 { CRASH_DUMP_EREADPID, "Failed to read real pid."},
94 { CRASH_UNWIND_ECONTEXT, "Unwind context illegal." },
95 { CRASH_UNWIND_EFRAME, "Failed to step ark js frame." },
96 { CRASH_UNWIND_ESTACK, "Stack corruption." },
97 { CRASH_LOG_ESTACKLOS, "Crash thread stack not found." },
98 { CRASH_LOG_ECHILDSTACK, "Child thread stack not found." },
99 { CRASH_LOG_EREGLOS, "Registers not found." },
100 { CRASH_LOG_EMEMLOS, "Memory not found." },
101 { CRASH_LOG_ESTACKMEMLOS, "Fault stack not found." },
102 { CRASH_LOG_EMAPLOS, "Maps not found." },
103 { CRASH_LOG_EHILOGLOS, "Hilog not found." },
104 { CRASH_LOG_ESUMMARYLOS, "Fault Summary not found." },
105 { CRASH_LOG_EPROCESS_LIFECYCLE, "Life Cycle timestamp invalid"},
106 };
107 for (uint8_t i = 0; i < sizeof(crashExceptions) / sizeof(crashExceptions[0]); i++) {
108 if (errCode == crashExceptions[i].errCode) {
109 return crashExceptions[i].msg;
110 }
111 }
112 return "Unknown reason.";
113 }
114
115 /**
116 * @brief Process crash dump exception description
117 */
118 typedef struct CrashDumpException {
119 /** request data head **/
120 RequestDataHead head;
121 /** Crash process id */
122 int32_t pid;
123 /** Crash process user id */
124 int32_t uid;
125 /** event happen time */
126 int64_t time;
127 /** Crash exception error code */
128 int32_t error;
129 /** Crash exception message */
130 char message[LINE_BUF_SIZE];
131 } __attribute__((packed)) CrashDumpException;;
132
133 #ifdef __cplusplus
134 }
135 #endif
136 #endif
137