1 /*
2  * Copyright (c) 2021-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 #ifndef DFX_DUMP_RES_H
16 #define DFX_DUMP_RES_H
17 
18 #include <cinttypes>
19 #include <string>
20 #include <sstream>
21 
22 namespace OHOS {
23 namespace HiviewDFX {
24 
25 /**
26  * @brief Processdump error code
27  * It describes the status of dumping.
28  */
29 enum DumpErrorCode : int32_t {
30     /** no error */
31     DUMP_ESUCCESS = 0,
32     /** read request error */
33     DUMP_EREADREQUEST,
34     /** ppid is crash */
35     DUMP_EGETPPID,
36     /** ptrace attach thread failed */
37     DUMP_EATTACH,
38     /** get fd error */
39     DUMP_EGETFD,
40     /** out of memory */
41     DUMP_ENOMEM,
42     /** bad register number */
43     DUMP_EBADREG,
44     /** attempt to write read-only register */
45     DUMP_EREADONLYREG,
46     /** stop unwinding */
47     DUMP_ESTOPUNWIND,
48     /** invalid IP */
49     DUMP_EINVALIDIP,
50     /** bad frame */
51     DUMP_EBADFRAME,
52     /** unsupported operation or bad value */
53     DUMP_EINVAL,
54     /** unwind info has unsupported version */
55     DUMP_EBADVERSION,
56     /** no unwind info found */
57     DUMP_ENOINFO,
58     /** no map info found */
59     DUMP_ENOMAP,
60     /** fail to read real pid */
61     DUMP_EREADPID,
62 };
63 
64 class DfxDumpRes {
65 public:
ToString(const int32_t & res)66     inline static std::string ToString(const int32_t& res)
67     {
68         std::stringstream ss;
69         ss << std::to_string(res) << " ( " << GetResStr(res) << " )";
70         return ss.str();
71     }
72 
73 private:
GetResStr(const int32_t & res)74     inline static const char* GetResStr(const int32_t& res)
75     {
76         const char *cp;
77         switch (res) {
78             case DUMP_ESUCCESS:     cp = "no error"; break;
79             case DUMP_EREADREQUEST: cp = "read dump request error"; break;
80             case DUMP_EGETPPID:     cp = "ppid is crashed before unwind"; break;
81             case DUMP_EATTACH:      cp = "ptrace attach thread failed"; break;
82             case DUMP_EGETFD:       cp = "get fd error"; break;
83             case DUMP_ENOMEM:       cp = "out of memory"; break;
84             case DUMP_EBADREG:      cp = "bad register number"; break;
85             case DUMP_EREADONLYREG: cp = "attempt to write read-only register"; break;
86             case DUMP_ESTOPUNWIND:  cp = "stop unwinding"; break;
87             case DUMP_EINVALIDIP:   cp = "invalid IP"; break;
88             case DUMP_EBADFRAME:    cp = "bad frame"; break;
89             case DUMP_EINVAL:       cp = "unsupported operation or bad value"; break;
90             case DUMP_EBADVERSION:  cp = "unwind info has unsupported version"; break;
91             case DUMP_ENOINFO:      cp = "no unwind info found"; break;
92             case DUMP_ENOMAP:       cp = "mapinfo is not exist"; break;
93             case DUMP_EREADPID:     cp = "fail to read real pid"; break;
94             default:                cp = "invalid error code"; break;
95         }
96         return cp;
97     }
98 };
99 } // namespace HiviewDFX
100 } // namespace OHOS
101 #endif
102