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 : #include <cstring>
12 : #include "exception_info_common.h"
13 : #include "kernel_symbol_locator.h"
14 : #include "runtime/rts/rts_kernel.h"
15 : #include "adump_pub.h"
16 : #include "str_utils.h"
17 : #include "log/adx_log.h"
18 : #include "log/hdc_log.h"
19 :
20 : namespace Adx {
21 : namespace {
22 : constexpr char MIX_AIC_SUFFIX[] = "_mix_aic";
23 : constexpr char MIX_AIV_SUFFIX[] = "_mix_aiv";
24 :
25 64 : const char* GetKernelMixSuffix(KernelMixType kernelMixType)
26 : {
27 64 : switch (kernelMixType) {
28 39 : case KernelMixType::AIC:
29 39 : return MIX_AIC_SUFFIX;
30 0 : case KernelMixType::AIV:
31 0 : return MIX_AIV_SUFFIX;
32 25 : default:
33 25 : return nullptr;
34 : }
35 : }
36 : } // namespace
37 :
38 262 : int32_t ExceptionInfoCommon::GetExceptionInfo(
39 : const rtExceptionInfo& exception, rtExceptionArgsInfo_t& exceptionArgsInfo)
40 : {
41 262 : rtExceptionExpandType_t exceptionTaskType = exception.expandInfo.type;
42 262 : if (exceptionTaskType == RT_EXCEPTION_AICORE) {
43 241 : exceptionArgsInfo = exception.expandInfo.u.aicoreInfo.exceptionArgs;
44 21 : } else if (exceptionTaskType == RT_EXCEPTION_FFTS_PLUS) {
45 12 : exceptionArgsInfo = exception.expandInfo.u.fftsPlusInfo.exceptionArgs;
46 9 : } else if (exceptionTaskType == RT_EXCEPTION_FUSION) {
47 8 : exceptionArgsInfo = exception.expandInfo.u.fusionInfo.u.aicoreCcuInfo.exceptionArgs;
48 : } else {
49 1 : IDE_LOGW(
50 : "Not support exception dump, type=%d(%s)", static_cast<int32_t>(exceptionTaskType),
51 : GetExceptionTaskTypeName(exception).c_str());
52 1 : return ADUMP_FAILED;
53 : }
54 :
55 261 : return ADUMP_SUCCESS;
56 : }
57 :
58 92 : std::string ExceptionInfoCommon::GetExceptionTaskTypeName(const rtExceptionInfo& exception)
59 : {
60 92 : switch (exception.expandInfo.type) {
61 8 : case RT_EXCEPTION_INVALID:
62 16 : return "invalid";
63 3 : case RT_EXCEPTION_FFTS_PLUS:
64 6 : return "fftsplus";
65 62 : case RT_EXCEPTION_AICORE:
66 124 : return "aicore";
67 3 : case RT_EXCEPTION_UB:
68 6 : return "ub";
69 1 : case RT_EXCEPTION_CCU:
70 2 : return "ccu";
71 3 : case RT_EXCEPTION_FUSION:
72 6 : return "fusion";
73 11 : case RT_EXCEPTION_AICPU:
74 22 : return "aicpu";
75 1 : default:
76 2 : return "unknown";
77 : }
78 : }
79 :
80 92 : bool ExceptionInfoCommon::IsSupportExceptionDump(const rtExceptionInfo& exception)
81 : {
82 92 : if (exception.retcode == ACL_ERROR_RT_AICORE_OVER_FLOW || exception.retcode == ACL_ERROR_RT_AIVEC_OVER_FLOW ||
83 : // Hardware faults do not need dump.
84 90 : exception.retcode == ACL_ERROR_RT_DEVICE_MEM_ERROR || exception.retcode == ACL_ERROR_RT_SUSPECT_REMOTE_ERROR ||
85 88 : exception.retcode == ACL_ERROR_RT_LINK_ERROR) {
86 5 : IDE_RUN_LOGI("Not support exception dump, rts retcode=%u.", exception.retcode);
87 5 : return false;
88 : }
89 :
90 87 : rtExceptionExpandType_t exceptionType = exception.expandInfo.type;
91 87 : if (exceptionType == RT_EXCEPTION_FFTS_PLUS || exceptionType == RT_EXCEPTION_AICORE ||
92 14 : exceptionType == RT_EXCEPTION_FUSION ||
93 : // Supported for callback exception dump.
94 : exceptionType == RT_EXCEPTION_AICPU) {
95 78 : return true;
96 : }
97 :
98 9 : IDE_RUN_LOGI(
99 : "Not support exception dump, type=%d(%s)", static_cast<int32_t>(exceptionType),
100 : GetExceptionTaskTypeName(exception).c_str());
101 9 : return false;
102 : }
103 :
104 62 : bool ExceptionInfoCommon::IsSupportDefaultExceptionDump(const rtExceptionInfo& exception)
105 : {
106 62 : rtExceptionExpandType_t exceptionType = exception.expandInfo.type;
107 62 : if (exceptionType == RT_EXCEPTION_FFTS_PLUS || exceptionType == RT_EXCEPTION_AICORE ||
108 : exceptionType == RT_EXCEPTION_FUSION) {
109 55 : return true;
110 : }
111 7 : IDE_LOGW(
112 : "Not support default exception dump, type=%d(%s)", static_cast<int32_t>(exceptionType),
113 : GetExceptionTaskTypeName(exception).c_str());
114 7 : return false;
115 : }
116 :
117 64 : std::string ExceptionInfoCommon::GetKernelNameWithoutMixSuffix(const std::string& kernelName)
118 : {
119 64 : const char* suffix = GetKernelMixSuffix(GetKernelMixType(kernelName));
120 64 : if (suffix == nullptr) {
121 25 : return kernelName;
122 : }
123 :
124 39 : std::string processedKernelName = kernelName;
125 39 : const size_t suffixLen = std::strlen(suffix);
126 39 : processedKernelName.resize(processedKernelName.size() - suffixLen);
127 39 : return processedKernelName;
128 39 : }
129 :
130 77 : std::string ExceptionInfoCommon::GetExceptionKernelName(const rtExceptionInfo& exception)
131 : {
132 77 : std::string rawKernelName;
133 77 : if (exception.expandInfo.type == RT_EXCEPTION_AICPU) {
134 7 : const rtAicpuExDetailInfo_t& aicpuInfo = exception.expandInfo.u.aicpuInfo;
135 7 : if (aicpuInfo.kernelName == nullptr) {
136 12 : return "";
137 : }
138 1 : rawKernelName.assign(aicpuInfo.kernelName);
139 : } else {
140 70 : rtExceptionArgsInfo_t exceptionArgsInfo{};
141 70 : if (GetExceptionInfo(exception, exceptionArgsInfo) != ADUMP_SUCCESS) {
142 2 : return "";
143 : }
144 :
145 69 : const rtExceptionKernelInfo_t& kernelInfo = exceptionArgsInfo.exceptionKernelInfo;
146 69 : if (kernelInfo.kernelName == nullptr || kernelInfo.kernelNameSize == 0) {
147 114 : return "";
148 : }
149 12 : rawKernelName.assign(kernelInfo.kernelName, kernelInfo.kernelNameSize);
150 : }
151 :
152 13 : return GetKernelNameWithoutMixSuffix(rawKernelName);
153 77 : }
154 :
155 7 : int32_t ExceptionInfoCommon::GetKernelDeviceAddr(rtBinHandle binHandle, void*& devAddr)
156 : {
157 7 : devAddr = nullptr;
158 7 : uint32_t binSize = 0;
159 7 : IDE_CTRL_VALUE_WARN(binHandle != nullptr, return ADUMP_FAILED, "bindHandle is nullptr.");
160 7 : rtError_t ret = rtsBinaryGetDevAddress(binHandle, &devAddr, &binSize);
161 7 : IDE_CTRL_VALUE_FAILED(
162 : ret == RT_ERROR_NONE && devAddr != nullptr, return ADUMP_FAILED,
163 : "rtsBinaryGetDevAddress failed, ret=%d, binHandle=%p, devAddr=%p.", static_cast<int32_t>(ret), binHandle,
164 : devAddr);
165 :
166 2 : IDE_LOGI("The device address of binHandle(%p) is %p, binSize=%u.", binHandle, devAddr, binSize);
167 2 : return ADUMP_SUCCESS;
168 : }
169 :
170 64 : KernelMixType ExceptionInfoCommon::GetKernelMixType(const std::string& kernelName)
171 : {
172 64 : if (kernelName.size() > std::strlen(MIX_AIC_SUFFIX) && StrUtils::EndsWith(kernelName, MIX_AIC_SUFFIX)) {
173 39 : return KernelMixType::AIC;
174 : }
175 25 : if (kernelName.size() > std::strlen(MIX_AIV_SUFFIX) && StrUtils::EndsWith(kernelName, MIX_AIV_SUFFIX)) {
176 0 : return KernelMixType::AIV;
177 : }
178 25 : return KernelMixType::NONE;
179 : }
180 :
181 73 : int32_t ExceptionInfoCommon::GetExceptionRegInfo(const rtExceptionInfo& exception, ExceptionRegInfo& exceptionRegInfo)
182 : {
183 73 : rtError_t rtRet = rtGetExceptionRegInfo(&exception, &exceptionRegInfo.errRegInfo, &exceptionRegInfo.coreNum);
184 73 : IDE_CTRL_VALUE_FAILED(
185 : rtRet == RT_ERROR_NONE, return ADUMP_FAILED, "rtGetExceptionRegInfo failed. ret: %d",
186 : static_cast<int32_t>(rtRet));
187 :
188 73 : IDE_LOGI("Get exception register information. coreNum=%u", exceptionRegInfo.coreNum);
189 73 : return ADUMP_SUCCESS;
190 : }
191 :
192 92 : int32_t ExceptionInfoCommon::GetBinDataFromHandle(rtBinHandle binHandle, std::string& binData, uint32_t& binSize)
193 : {
194 92 : IDE_CTRL_VALUE_FAILED(binHandle != nullptr, return ADUMP_FAILED, "binHandle is null");
195 :
196 54 : void* bufAddr = nullptr;
197 54 : uint32_t bufSize = 0;
198 54 : rtError_t ret = rtGetBinBuffer(binHandle, RT_BIN_HOST_ADDR, &bufAddr, &bufSize);
199 54 : IDE_CTRL_VALUE_FAILED(
200 : ret == RT_ERROR_NONE && bufAddr != nullptr && bufSize != 0, return ADUMP_FAILED,
201 : "rtGetBinBuffer failed, ret=%d, binHandle=%p, bufAddr=%p, bufSize=%u.", static_cast<int32_t>(ret), binHandle,
202 : bufAddr, bufSize);
203 :
204 52 : IDE_LOGI("Get kernel bin data success. binHandle=%p, bufAddr=%p, bufSize=%u", binHandle, bufAddr, bufSize);
205 52 : binData.assign(reinterpret_cast<char*>(bufAddr), bufSize);
206 52 : binSize = bufSize;
207 52 : return ADUMP_SUCCESS;
208 : }
209 :
210 : } // namespace Adx
|