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 668 : explicit HcclException(const ExceptionType &exceptionType, const std::string &userDefinedMsg)
25 668 : : exceptionType(exceptionType), userDefinedMsg(userDefinedMsg),
26 1336 : errorMsg(ExceptionInfo::GetErrorMsg(exceptionType) + userDefinedMsg) {
27 668 : StoreBackTrace();
28 668 : }
29 :
30 137 : const char *what() const noexcept override {
31 137 : return errorMsg.c_str();
32 : }
33 :
34 52 : HcclResult GetErrorCode() const {
35 52 : return ExceptionInfo::GetErrorCode(exceptionType);
36 : }
37 :
38 34 : std::vector<std::string> GetBackTraceStrings() const {
39 34 : return backtraceStrings;
40 : }
41 :
42 : private:
43 668 : void StoreBackTrace() {
44 668 : constexpr int BACKTRACE_DEPTH = 15;
45 : void *array[BACKTRACE_DEPTH];
46 668 : int size = backtrace(array, BACKTRACE_DEPTH);
47 :
48 668 : char **callBackStrings = backtrace_symbols(array, size);
49 668 : if (callBackStrings == nullptr) {
50 0 : backtraceStrings.emplace_back("Failed to get backtrace symbols");
51 0 : return;
52 : }
53 :
54 10688 : for (auto i = 0; i < size; ++i) {
55 10020 : backtraceStrings.emplace_back(callBackStrings[i]);
56 : }
57 668 : free(callBackStrings);
58 : };
59 :
60 : std::vector<std::string> backtraceStrings;
61 : ExceptionType exceptionType;
62 : std::string userDefinedMsg{""};
63 : std::string errorMsg{""};
64 : };
65 :
66 : } // namespace Hccl
67 :
68 : #endif // HCCL_HCCL_EXCEPTION_H
|