Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 :
11 : #ifndef HCCL_HCCL_EXCEPTION_H
12 : #define HCCL_HCCL_EXCEPTION_H
13 :
14 : #include <exception>
15 : #include <string>
16 : #include <vector>
17 : #include "exception_defination.h"
18 :
19 : #include <execinfo.h>
20 :
21 : namespace Hccl {
22 : class HcclException : public std::exception {
23 : public:
24 707 : explicit HcclException(const ExceptionType& exceptionType, const std::string& userDefinedMsg)
25 1414 : : exceptionType(exceptionType),
26 707 : userDefinedMsg(userDefinedMsg),
27 1414 : errorMsg(ExceptionInfo::GetErrorMsg(exceptionType) + userDefinedMsg)
28 : {
29 707 : StoreBackTrace();
30 707 : }
31 :
32 186 : const char* what() const noexcept override { return errorMsg.c_str(); }
33 :
34 50 : HcclResult GetErrorCode() const { return ExceptionInfo::GetErrorCode(exceptionType); }
35 :
36 32 : std::vector<std::string> GetBackTraceStrings() const { return backtraceStrings; }
37 :
38 : private:
39 707 : void StoreBackTrace()
40 : {
41 707 : constexpr int BACKTRACE_DEPTH = 15;
42 : void* array[BACKTRACE_DEPTH];
43 707 : int size = backtrace(array, BACKTRACE_DEPTH);
44 :
45 707 : char** callBackStrings = backtrace_symbols(array, size);
46 707 : if (callBackStrings == nullptr) {
47 0 : backtraceStrings.emplace_back("Failed to get backtrace symbols");
48 0 : return;
49 : }
50 :
51 11312 : for (auto i = 0; i < size; ++i) {
52 10605 : backtraceStrings.emplace_back(callBackStrings[i]);
53 : }
54 707 : free(callBackStrings);
55 : };
56 :
57 : std::vector<std::string> backtraceStrings;
58 : ExceptionType exceptionType;
59 : std::string userDefinedMsg{""};
60 : std::string errorMsg{""};
61 : };
62 :
63 : } // namespace Hccl
64 :
65 : #endif // HCCL_HCCL_EXCEPTION_H
|