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 659 : explicit HcclException(const ExceptionType &exceptionType, const std::string &userDefinedMsg)
25 659 : : exceptionType(exceptionType), userDefinedMsg(userDefinedMsg),
26 1318 : errorMsg(ExceptionInfo::GetErrorMsg(exceptionType) + userDefinedMsg) {
27 659 : StoreBackTrace();
28 659 : }
29 :
30 129 : const char *what() const noexcept override {
31 129 : return errorMsg.c_str();
32 : }
33 :
34 49 : HcclResult GetErrorCode() const {
35 49 : return ExceptionInfo::GetErrorCode(exceptionType);
36 : }
37 :
38 31 : std::vector<std::string> GetBackTraceStrings() const {
39 31 : return backtraceStrings;
40 : }
41 :
42 : private:
43 659 : void StoreBackTrace() {
44 659 : constexpr int BACKTRACE_DEPTH = 15;
45 : void *array[BACKTRACE_DEPTH];
46 659 : int size = backtrace(array, BACKTRACE_DEPTH);
47 :
48 659 : char **callBackStrings = backtrace_symbols(array, size);
49 659 : if (callBackStrings == nullptr) {
50 0 : backtraceStrings.emplace_back("Failed to get backtrace symbols");
51 0 : return;
52 : }
53 :
54 10544 : for (auto i = 0; i < size; ++i) {
55 9885 : backtraceStrings.emplace_back(callBackStrings[i]);
56 : }
57 659 : 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
|