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 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 enum CrashExceptionCode : int32_t {
27     CRASH_ESUCCESS = 0,                 /* No error */
28 
29     CRASH_SIGNAL_EMASKED = 101,         /* Signal has been masked */
30     CRASH_SIGNAL_EFORK,                 /* Failed to fork child process */
31     CRASH_SIGNAL_ECLONE,                /* Failed to clone thread of recycle dump process */
32     CRASH_SIGNAL_ESETSTATE,             /* Failed to set dump state */
33     CRASH_SIGNAL_EINHERITCAP,           /* Failed to inherit capabilities */
34     CRASH_SIGNAL_EEXECL,                /* Failed to execl processdump */
35     CRASH_SIGNAL_EWAITEXIT,             /* Failed to wait vm process exit */
36     CRASH_SIGNAL_EREADPIPE,             /* Failed to read pipe due to timeout */
37     CRASH_SIGNAL_ECREATEPIPE,           /* Failed to init create pipe */
38 
39     CRASH_DUMP_EREADREQ = 201,          /* Failed to read dump request */
40     CRASH_DUMP_EPARENTPID,              /* Failed to check parent pid */
41     CRASH_DUMP_EATTACH,                 /* Failed to attach target process */
42     CRASH_DUMP_EWRITEFD,                /* Failed to request writen fd */
43     CRASH_DUMP_EKILLED,                 /* Tagert process has been killed */
44     CRASH_DUMP_LOCAL_REPORT,            /* Local Handler DumpInfo Report*/
45     CRASH_DUMP_EREADPID,                /* Failed to read real pid*/
46 
47     CRASH_UNWIND_ECONTEXT = 301,        /* Unwind context illegal */
48     CRASH_UNWIND_EFRAME,                /* Failed to step ark js frame */
49     CRASH_UNWIND_ESTACK,                /* Stack corruption */
50 
51     CRASH_LOG_ESTACKLOS = 401,          /* Crash thread stack not found */
52     CRASH_LOG_ECHILDSTACK,              /* Child thread stack not found */
53     CRASH_LOG_EREGLOS,                  /* Registers not found */
54     CRASH_LOG_EMEMLOS,                  /* Memory not found */
55     CRASH_LOG_ESTACKMEMLOS,             /* Fault stack not found */
56     CRASH_LOG_EMAPLOS,                  /* Maps not found */
57     CRASH_LOG_EHILOGLOS,                /* Hilog not found */
58     CRASH_LOG_ESUMMARYLOS,              /* Fault Summary not found */
59 
60     CRASH_UNKNOWN = 500,                /* Unknown reason */
61 };
62 
63 struct ErrCodeToStr {
64     /** Crash exception stage code */
65     int32_t errCode;
66     /** Crash exception string */
67     const char* str;
68 };
69 
70 static struct ErrCodeToStr g_crashExceptionMap[] = {
71     {CRASH_SIGNAL_EMASKED,      "Signal has been masked." },
72     {CRASH_SIGNAL_EFORK,        "Failed to fork child process." },
73     {CRASH_SIGNAL_ECLONE,       "Failed to clone thread of recycle dump process." },
74     {CRASH_SIGNAL_ESETSTATE,    "Failed to set dump state." },
75     {CRASH_SIGNAL_EINHERITCAP,  "Failed to inherit capabilities." },
76     {CRASH_SIGNAL_EEXECL,       "Failed to execl processdump." },
77     {CRASH_SIGNAL_EWAITEXIT,    "Failed to wait vm process exit." },
78     {CRASH_SIGNAL_EREADPIPE,    "Failed to read pipe due to timeout."},
79     {CRASH_SIGNAL_ECREATEPIPE,  "Failed to init create pipe."},
80     {CRASH_DUMP_EREADREQ,       "Failed to read dump request." },
81     {CRASH_DUMP_EPARENTPID,     "Failed to check parent pid." },
82     {CRASH_DUMP_EATTACH,        "Failed to attach target process." },
83     {CRASH_DUMP_EWRITEFD,       "Failed to request writen fd." },
84     {CRASH_DUMP_EKILLED,        "Tagert process has been killed." },
85     {CRASH_DUMP_EREADPID,       "Failed to read real pid."},
86     {CRASH_UNWIND_ECONTEXT,     "Unwind context illegal." },
87     {CRASH_UNWIND_EFRAME,       "Failed to step ark js frame." },
88     {CRASH_UNWIND_ESTACK,       "Stack corruption." },
89     {CRASH_LOG_ESTACKLOS,       "Crash thread stack not found." },
90     {CRASH_LOG_ECHILDSTACK,     "Child thread stack not found." },
91     {CRASH_LOG_EREGLOS,         "Registers not found." },
92     {CRASH_LOG_EMEMLOS,         "Memory not found." },
93     {CRASH_LOG_ESTACKMEMLOS,    "Fault stack not found." },
94     {CRASH_LOG_EMAPLOS,         "Maps not found." },
95     {CRASH_LOG_EHILOGLOS,       "Hilog not found." },
96     {CRASH_LOG_ESUMMARYLOS,     "Fault Summary not found."},
97     {CRASH_UNKNOWN,             "Unknown reason." },
98 };
99 
100 /**
101  * @brief Process crash dump exception description
102 */
103 struct CrashDumpException {
104     /** Crash process id */
105     int32_t pid;
106     /** Crash process user id */
107     int32_t uid;
108     /** event happen time */
109     int64_t time;
110     /** Crash exception error code */
111     int32_t error;
112     /** Crash exception message */
113     char message[LINE_BUF_SIZE];
114 };
115 
116 #ifdef __cplusplus
117 }
118 #endif
119 #endif
120