| @@ -36,6 +36,7 @@ set(ascendDumpBaseSrcList | |||
| 36 | ${ADUMP_ADUMP_DIR}/exception/exception_info_common.cpp | 36 | ${ADUMP_ADUMP_DIR}/exception/exception_info_common.cpp |
| 37 | ${ADUMP_ADUMP_DIR}/exception/kernel_info_collector.cpp | 37 | ${ADUMP_ADUMP_DIR}/exception/kernel_info_collector.cpp |
| 38 | ${ADUMP_ADUMP_DIR}/exception/kernel_symbol_locator.cpp | 38 | ${ADUMP_ADUMP_DIR}/exception/kernel_symbol_locator.cpp |
| 39 | + ${ADUMP_ADUMP_DIR}/exception/kernel_source_symbolizer.cpp | ||
| 39 | ${ADUMP_ADUMP_DIR}/exception/kernel_pc_fixer.cpp | 40 | ${ADUMP_ADUMP_DIR}/exception/kernel_pc_fixer.cpp |
| 40 | ${ADUMP_ADUMP_DIR}/exception/register_config/register_config.cpp | 41 | ${ADUMP_ADUMP_DIR}/exception/register_config/register_config.cpp |
| 41 | ${ADUMP_ADUMP_DIR}/exception/thread_manager.cpp | 42 | ${ADUMP_ADUMP_DIR}/exception/thread_manager.cpp |
| @@ -45,8 +45,8 @@ int32_t DumpArgsCallback::DumpKernelBin() | |||
| 45 | IDE_CTRL_VALUE_FAILED(ret == ADUMP_SUCCESS, return ADUMP_FAILED, | 45 | IDE_CTRL_VALUE_FAILED(ret == ADUMP_SUCCESS, return ADUMP_FAILED, |
| 46 | "Init for dump kernel bin failed. ret=%d, bin=%p, kernelName=%s.", ret, info_.bin, kernelName.c_str()); | 46 | "Init for dump kernel bin failed. ret=%d, bin=%p, kernelName=%s.", ret, info_.bin, kernelName.c_str()); |
| 47 | 47 | ||
| 48 | - // 先同步落 _host.o,再做慢的 kernel_meta 搜索拷贝。落盘失败需传播,保持与拆分前一致的错误可观测性。 | 48 | + std::string hostOPath; |
| 49 | - ret = collector.DumpHostKernelBin(dumpPath_); | 49 | + ret = collector.DumpHostKernelBin(dumpPath_, hostOPath); |
| 50 | IDE_CTRL_VALUE_WARN(ret == ADUMP_SUCCESS, return ADUMP_FAILED, | 50 | IDE_CTRL_VALUE_WARN(ret == ADUMP_SUCCESS, return ADUMP_FAILED, |
| 51 | "DumpHostKernelBin failed, kernelName=%s.", kernelName.c_str()); | 51 | "DumpHostKernelBin failed, kernelName=%s.", kernelName.c_str()); |
| 52 | 52 | ||
| @@ -58,7 +58,7 @@ int32_t DumpArgsCallback::DumpKernelBin() | |||
| 58 | return ADUMP_SUCCESS; | 58 | return ADUMP_SUCCESS; |
| 59 | } | 59 | } |
| 60 | 60 | ||
| 61 | -int32_t DumpArgsCallback::DumpKernelErrorSymbols() | 61 | +int32_t DumpArgsCallback::DumpKernelErrorSymbols(ErrorLocation &outLocation) |
| 62 | { | 62 | { |
| 63 | if (info_.bin == nullptr) { | 63 | if (info_.bin == nullptr) { |
| 64 | return ADUMP_SUCCESS; | 64 | return ADUMP_SUCCESS; |
| @@ -69,14 +69,23 @@ int32_t DumpArgsCallback::DumpKernelErrorSymbols() | |||
| 69 | "KernelSymbolLocator InitFromBinHandle failed for callback exception. ret=%d", ret); | 69 | "KernelSymbolLocator InitFromBinHandle failed for callback exception. ret=%d", ret); |
| 70 | locator.UpdateStartPCFromDeviceAddr(info_.bin); | 70 | locator.UpdateStartPCFromDeviceAddr(info_.bin); |
| 71 | 71 | ||
| 72 | + // 回调路径的 _host.o 已由 DumpKernelBin 同步落盘,此处复用其路径决定 symbolize 用的 .o。 | ||
| 73 | + // kernelName 是定长数组成员,不会为 nullptr;std::string 构造依赖其内部 '\0' 结尾。 | ||
| 74 | + KernelInfoCollector collector; | ||
| 75 | + if (collector.InitFromBinHandle(info_.bin, std::string(info_.kernelName)) == ADUMP_SUCCESS) { | ||
| 76 | + const std::string hostOPath = collector.GetHostOFilePath(dumpPath_); | ||
| 77 | + locator.SetOFilePath(KernelSymbolLocator::ResolveOFilePath(hostOPath)); | ||
| 78 | + } | ||
| 79 | + | ||
| 72 | ExceptionRegInfo exceptionRegInfo{0, nullptr}; | 80 | ExceptionRegInfo exceptionRegInfo{0, nullptr}; |
| 73 | ret = ExceptionInfoCommon::GetExceptionRegInfo(exception_, exceptionRegInfo); | 81 | ret = ExceptionInfoCommon::GetExceptionRegInfo(exception_, exceptionRegInfo); |
| 74 | IDE_CTRL_VALUE_WARN(ret == ADUMP_SUCCESS, return ADUMP_FAILED, | 82 | IDE_CTRL_VALUE_WARN(ret == ADUMP_SUCCESS, return ADUMP_FAILED, |
| 75 | "Get exception register information failed for callback exception. ret=%d", ret); | 83 | "Get exception register information failed for callback exception. ret=%d", ret); |
| 76 | 84 | ||
| 77 | - ret = locator.LocateAndPrintErrorSymbolsForCore(info_.coreId, info_.coreType, exceptionRegInfo); | 85 | + // 构建 ErrorLocation:LocateErrorSymbolsForCore 内部定位偏移后对该 .o 批量 symbolize 并回填 src。 |
| 86 | + ret = locator.LocateErrorSymbolsForCore(info_.coreId, info_.coreType, exceptionRegInfo, outLocation); | ||
| 78 | IDE_CTRL_VALUE_WARN(ret == ADUMP_SUCCESS, return ADUMP_FAILED, | 87 | IDE_CTRL_VALUE_WARN(ret == ADUMP_SUCCESS, return ADUMP_FAILED, |
| 79 | - "LocateAndPrintErrorSymbolsForCore failed for callback exception. ret=%d, coreId=%u, coreType=%u.", | 88 | + "LocateErrorSymbolsForCore failed for callback exception. ret=%d, coreId=%u, coreType=%u.", |
| 80 | ret, info_.coreId, info_.coreType); | 89 | ret, info_.coreId, info_.coreType); |
| 81 | 90 | ||
| 82 | return ADUMP_SUCCESS; | 91 | return ADUMP_SUCCESS; |
| @@ -136,7 +145,9 @@ int32_t DumpArgsCallback::QueryDfxInfo(std::vector<uint8_t> &dfxBuffer) | |||
| 136 | 145 | ||
| 137 | int32_t DumpArgsCallback::DumpDfxArgs() | 146 | int32_t DumpArgsCallback::DumpDfxArgs() |
| 138 | { | 147 | { |
| 139 | - if (info_.argAddr == nullptr || info_.argSize == 0 || info_.bin == nullptr || info_.kernelName[0] == '\0') { | 148 | + // kernelName 是定长数组成员,不会为 nullptr,直接判首字节是否为空串。 |
| 149 | + if (info_.argAddr == nullptr || info_.argSize == 0 || info_.bin == nullptr || | ||
| 150 | + info_.kernelName[0] == '\0') { | ||
| 140 | return ADUMP_SUCCESS; | 151 | return ADUMP_SUCCESS; |
| 141 | } | 152 | } |
| 142 | 153 | ||
| @@ -19,6 +19,7 @@ | |||
| 19 | #include "dump_args.h" | 19 | #include "dump_args.h" |
| 20 | #include "dump_file.h" | 20 | #include "dump_file.h" |
| 21 | #include "dump_tensor.h" | 21 | #include "dump_tensor.h" |
| 22 | +#include "kernel_symbol_locator.h" | ||
| 22 | #include "runtime/base.h" | 23 | #include "runtime/base.h" |
| 23 | #include "sys_utils.h" | 24 | #include "sys_utils.h" |
| 24 | #include "log/hdc_log.h" | 25 | #include "log/hdc_log.h" |
| @@ -32,7 +33,7 @@ public: | |||
| 32 | int32_t DumpExtraTensors(); | 33 | int32_t DumpExtraTensors(); |
| 33 | int32_t Dump(); | 34 | int32_t Dump(); |
| 34 | int32_t DumpKernelBin(); | 35 | int32_t DumpKernelBin(); |
| 35 | - int32_t DumpKernelErrorSymbols(); | 36 | + int32_t DumpKernelErrorSymbols(ErrorLocation &outLocation); |
| 36 | 37 | ||
| 37 | private: | 38 | private: |
| 38 | int32_t QueryDfxInfo(std::vector<uint8_t> &dfxBuffer); | 39 | int32_t QueryDfxInfo(std::vector<uint8_t> &dfxBuffer); |
| @@ -22,7 +22,7 @@ namespace Adx { | |||
| 22 | int32_t DumpCore::DumpCoreFile(const rtExceptionInfo &exception) | 22 | int32_t DumpCore::DumpCoreFile(const rtExceptionInfo &exception) |
| 23 | { | 23 | { |
| 24 | if (ExceptionInfoCommon::GetExceptionRegInfo(exception, exceptionRegInfo_) == ADUMP_SUCCESS) { | 24 | if (ExceptionInfoCommon::GetExceptionRegInfo(exception, exceptionRegInfo_) == ADUMP_SUCCESS) { |
| 25 | - KernelSymbolLocator::DumpErrorSymbols(exception, exceptionRegInfo_); | 25 | + KernelSymbolLocator::DumpErrorSymbols(exception, exceptionRegInfo_, path_); |
| 26 | } | 26 | } |
| 27 | DumpCoreInfo(exception.deviceid); | 27 | DumpCoreInfo(exception.deviceid); |
| 28 | DumpGlobalMemory(exception); | 28 | DumpGlobalMemory(exception); |
| @@ -291,7 +291,8 @@ int32_t DumpOperator::DumpException(const uint32_t deviceId, const std::string & | |||
| 291 | ret = ADUMP_FAILED; | 291 | ret = ADUMP_FAILED; |
| 292 | } | 292 | } |
| 293 | // 先同步落 _host.o,再做慢的 kernel_meta 搜索拷贝。落盘失败需传播,保持与拆分前一致的错误可观测性。 | 293 | // 先同步落 _host.o,再做慢的 kernel_meta 搜索拷贝。落盘失败需传播,保持与拆分前一致的错误可观测性。 |
| 294 | - if (kernelCollector_.DumpHostKernelBin(dumpPath) != ADUMP_SUCCESS) { | 294 | + std::string hostOPath; |
| 295 | + if (kernelCollector_.DumpHostKernelBin(dumpPath, hostOPath) != ADUMP_SUCCESS) { | ||
| 295 | ret = ADUMP_FAILED; | 296 | ret = ADUMP_FAILED; |
| 296 | } | 297 | } |
| 297 | if (kernelCollector_.StartCollectKernel(dumpPath) != ADUMP_SUCCESS) { | 298 | if (kernelCollector_.StartCollectKernel(dumpPath) != ADUMP_SUCCESS) { |
| @@ -22,6 +22,7 @@ | |||
| 22 | #include "exception_info_common.h" | 22 | #include "exception_info_common.h" |
| 23 | #include "kernel_symbol_locator.h" | 23 | #include "kernel_symbol_locator.h" |
| 24 | #include "kernel_info_collector.h" | 24 | #include "kernel_info_collector.h" |
| 25 | +#include "kernel_source_symbolizer.h" | ||
| 25 | #include "exception_dumper.h" | 26 | #include "exception_dumper.h" |
| 26 | 27 | ||
| 27 | namespace Adx { | 28 | namespace Adx { |
| @@ -326,16 +327,20 @@ void ExceptionDumper::DumpHostKernelBinBeforeSymbolize(const rtExceptionInfo &ex | |||
| 326 | IDE_CTRL_VALUE_WARN(ret == ADUMP_SUCCESS, return, | 327 | IDE_CTRL_VALUE_WARN(ret == ADUMP_SUCCESS, return, |
| 327 | "Load kernel bin buffer failed, skip early dump host kernel bin."); | 328 | "Load kernel bin buffer failed, skip early dump host kernel bin."); |
| 328 | 329 | ||
| 329 | - // 提前同步落 _host.o;DumpHostKernelBin 幂等,后置慢搜索路径已存在则会跳过。 | 330 | + // 提前无条件同步落 _host.o(只依赖 kernel bin buffer,不依赖 ELF 符号解析), |
| 330 | - (void)collector.DumpHostKernelBin(dumpPath); | 331 | + // 保证符号表损坏等场景下 _host.o 仍能落盘供事后分析;DumpHostKernelBin 幂等, |
| 332 | + // 后续 symbolize/慢搜索路径复用已落盘文件不会重复写。 | ||
| 333 | + std::string hostOPath; | ||
| 334 | + (void)collector.DumpHostKernelBin(dumpPath, hostOPath); | ||
| 331 | } | 335 | } |
| 332 | 336 | ||
| 333 | int32_t ExceptionDumper::DumpNormalException(const rtExceptionInfo &exception, const std::string &dumpPath) | 337 | int32_t ExceptionDumper::DumpNormalException(const rtExceptionInfo &exception, const std::string &dumpPath) |
| 334 | { | 338 | { |
| 335 | IDE_CTRL_VALUE_WARN(ExceptionInfoCommon::IsSupportDefaultExceptionDump(exception), | 339 | IDE_CTRL_VALUE_WARN(ExceptionInfoCommon::IsSupportDefaultExceptionDump(exception), |
| 336 | return ADUMP_FAILED, "Exception is not support default dump."); | 340 | return ADUMP_FAILED, "Exception is not support default dump."); |
| 341 | + // 先无条件提前落 _host.o,再符号化(symbolize 复用已落盘文件,不重复落盘)。 | ||
| 337 | DumpHostKernelBinBeforeSymbolize(exception, dumpPath); | 342 | DumpHostKernelBinBeforeSymbolize(exception, dumpPath); |
| 338 | - KernelSymbolLocator::DumpErrorSymbols(exception); | 343 | + KernelSymbolLocator::DumpErrorSymbols(exception, dumpPath); |
| 339 | DumpOperator excOp; | 344 | DumpOperator excOp; |
| 340 | bool find = FindExceptionOperator(exception, excOp); | 345 | bool find = FindExceptionOperator(exception, excOp); |
| 341 | if (!find) { | 346 | if (!find) { |
| @@ -362,8 +367,9 @@ int32_t ExceptionDumper::DumpArgsExceptionDefault(const rtExceptionInfo &excepti | |||
| 362 | { | 367 | { |
| 363 | IDE_CTRL_VALUE_WARN(ExceptionInfoCommon::IsSupportDefaultExceptionDump(exception), | 368 | IDE_CTRL_VALUE_WARN(ExceptionInfoCommon::IsSupportDefaultExceptionDump(exception), |
| 364 | return ADUMP_FAILED, "Exception is not support default dump."); | 369 | return ADUMP_FAILED, "Exception is not support default dump."); |
| 370 | + // 先无条件提前落 _host.o,再符号化(symbolize 复用已落盘文件,不重复落盘)。 | ||
| 365 | DumpHostKernelBinBeforeSymbolize(exception, dumpPath); | 371 | DumpHostKernelBinBeforeSymbolize(exception, dumpPath); |
| 366 | - KernelSymbolLocator::DumpErrorSymbols(exception); | 372 | + KernelSymbolLocator::DumpErrorSymbols(exception, dumpPath); |
| 367 | DumpArgs args; | 373 | DumpArgs args; |
| 368 | if (args.LoadArgsExceptionInfo(exception) != ADUMP_SUCCESS) { | 374 | if (args.LoadArgsExceptionInfo(exception) != ADUMP_SUCCESS) { |
| 369 | return ADUMP_FAILED; | 375 | return ADUMP_FAILED; |
| @@ -468,6 +474,8 @@ void ExceptionDumper::DumpCallbackData(const rtExceptionInfo &exception, | |||
| 468 | const std::vector<ExceptionDumpInfo> &dumpInfos, | 474 | const std::vector<ExceptionDumpInfo> &dumpInfos, |
| 469 | const std::string &dumpPath) | 475 | const std::string &dumpPath) |
| 470 | { | 476 | { |
| 477 | + // 收集每个回调 info(单核) 的定位结果,循环结束后按 (.o, 偏移) 聚类打印。 | ||
| 478 | + std::vector<ErrorLocation> allLocations; | ||
| 471 | for (const auto &info : dumpInfos) { | 479 | for (const auto &info : dumpInfos) { |
| 472 | IDE_LOGE("[Dump][Exception] Begin to dump callback exception. coreType=%u, coreId=%u, " | 480 | IDE_LOGE("[Dump][Exception] Begin to dump callback exception. coreType=%u, coreId=%u, " |
| 473 | "argAddr=%p, argSize=%u, binHandle=%p, extraTensorNum=%u, kernelName=%s.", | 481 | "argAddr=%p, argSize=%u, binHandle=%p, extraTensorNum=%u, kernelName=%s.", |
| @@ -489,18 +497,27 @@ void ExceptionDumper::DumpCallbackData(const rtExceptionInfo &exception, | |||
| 489 | IDE_LOGW("Dump callback data to file failed."); | 497 | IDE_LOGW("Dump callback data to file failed."); |
| 490 | } | 498 | } |
| 491 | 499 | ||
| 500 | + // 先落 _host.o(同步),DumpKernelErrorSymbols 内定位偏移后对其批量 symbolize。 | ||
| 492 | ret = argsCallback.DumpKernelBin(); | 501 | ret = argsCallback.DumpKernelBin(); |
| 493 | if (ret != ADUMP_SUCCESS) { | 502 | if (ret != ADUMP_SUCCESS) { |
| 494 | IDE_LOGW("Dump callback kernel bin failed."); | 503 | IDE_LOGW("Dump callback kernel bin failed."); |
| 495 | } | 504 | } |
| 496 | 505 | ||
| 497 | - ret = argsCallback.DumpKernelErrorSymbols(); | 506 | + ErrorLocation location; |
| 507 | + ret = argsCallback.DumpKernelErrorSymbols(location); | ||
| 498 | if (ret != ADUMP_SUCCESS) { | 508 | if (ret != ADUMP_SUCCESS) { |
| 499 | IDE_LOGW("Dump callback kernel error symbols failed."); | 509 | IDE_LOGW("Dump callback kernel error symbols failed."); |
| 510 | + } else { | ||
| 511 | + allLocations.push_back(location); | ||
| 500 | } | 512 | } |
| 501 | 513 | ||
| 502 | IDE_LOGI("Dump callback data finished, kernelName=%s.", info.kernelName); | 514 | IDE_LOGI("Dump callback data finished, kernelName=%s.", info.kernelName); |
| 503 | } | 515 | } |
| 516 | + // 各 core 的源码解析已在 DumpKernelErrorSymbols → 批量 symbolize 内完成并回填, | ||
| 517 | + // 此处只在工具可用时统一打印聚类汇总(工具不可用时汇总无源码信息、无增量价值)。 | ||
| 518 | + if (KernelSourceSymbolizer::IsAvailable()) { | ||
| 519 | + KernelSymbolLocator::PrintClassificationSummary(allLocations); | ||
| 520 | + } | ||
| 504 | KernelSymbolLocator::ClearCache(); | 521 | KernelSymbolLocator::ClearCache(); |
| 505 | IDE_LOGI("Dump all callback data success."); | 522 | IDE_LOGI("Dump all callback data success."); |
| 506 | } | 523 | } |
| @@ -39,8 +39,8 @@ constexpr uint64_t JSON_SUFFIX_LEN = 5; // length of ".json" | |||
| 39 | 39 | ||
| 40 | int32_t StartCollectKernelAsync(std::shared_ptr<KernelInfoCollector> collector, const std::string &dumpPath) { | 40 | int32_t StartCollectKernelAsync(std::shared_ptr<KernelInfoCollector> collector, const std::string &dumpPath) { |
| 41 | int32_t ret = collector->LoadKernelBinBuffer(); | 41 | int32_t ret = collector->LoadKernelBinBuffer(); |
| 42 | - // 先同步落 _host.o(纯内存 buffer 写,快),再把慢的 kernel_meta 搜索丢到异步线程。 | 42 | + // _host.o 已由编排层(DumpHostKernelBinBeforeSymbolize)在符号化前无条件同步落盘, |
| 43 | - (void)collector->DumpHostKernelBin(dumpPath); | 43 | + // 这里不再重复落盘,只把慢的 kernel_meta 搜索拷贝丢到异步线程。 |
| 44 | IDE_LOGD("Start collecting kernel file."); | 44 | IDE_LOGD("Start collecting kernel file."); |
| 45 | std::thread collectKernel([collector, dumpPath](){ | 45 | std::thread collectKernel([collector, dumpPath](){ |
| 46 | int32_t tid = mmGetTid(); | 46 | int32_t tid = mmGetTid(); |
| @@ -224,14 +224,25 @@ std::string KernelInfoCollector::SearchJsonFiles(const std::string &rootPath, co | |||
| 224 | return ""; | 224 | return ""; |
| 225 | } | 225 | } |
| 226 | 226 | ||
| 227 | +std::string KernelInfoCollector::GetHostOFilePath(const std::string &dumpPath) const | ||
| 228 | +{ | ||
| 229 | + std::string kernelName = GetProcessedKernelName(); | ||
| 230 | + Path hostKernelBinPath(dumpPath); | ||
| 231 | + std::string hostKernelBinName = StrUtils::Replace(kernelName, INVALID_FILE_NAME_CHAR, '_') + "_host.o"; | ||
| 232 | + hostKernelBinPath.Concat(hostKernelBinName); | ||
| 233 | + return hostKernelBinPath.GetString(); | ||
| 234 | +} | ||
| 235 | + | ||
| 227 | /** | 236 | /** |
| 228 | * @brief : dump the kernel bin file runtime load from host | 237 | * @brief : dump the kernel bin file runtime load from host |
| 229 | * @param [in] : dumpPath path of the exception dump file | 238 | * @param [in] : dumpPath path of the exception dump file |
| 239 | + * @param [out] : outHostOPath 绝对/拼接后的 _host.o 落盘路径,供后续 symbolize 使用 | ||
| 230 | * @return : ADUMP_SUCCESS succeed; ADUMP_FAILED failed | 240 | * @return : ADUMP_SUCCESS succeed; ADUMP_FAILED failed |
| 231 | * @note : 从 StartCollectKernel 拆出,供调用方在慢搜索/修正 PC 之前单独同步落 _host.o。 | 241 | * @note : 从 StartCollectKernel 拆出,供调用方在慢搜索/修正 PC 之前单独同步落 _host.o。 |
| 232 | */ | 242 | */ |
| 233 | -int32_t KernelInfoCollector::DumpHostKernelBin(const std::string &dumpPath) const | 243 | +int32_t KernelInfoCollector::DumpHostKernelBin(const std::string &dumpPath, std::string &outHostOPath) const |
| 234 | { | 244 | { |
| 245 | + outHostOPath.clear(); | ||
| 235 | // 与拆分前 StartCollectKernel 入口保持一致的前置校验:kernelName 为空会退化成非唯一的 "_host.o", | 246 | // 与拆分前 StartCollectKernel 入口保持一致的前置校验:kernelName 为空会退化成非唯一的 "_host.o", |
| 236 | // 不同异常/算子会互相覆盖并污染幂等判断,故此处直接跳过。 | 247 | // 不同异常/算子会互相覆盖并污染幂等判断,故此处直接跳过。 |
| 237 | if (kernelName_.empty() || kernelBinHandle_ == nullptr || kernelBinSize_ == 0) { | 248 | if (kernelName_.empty() || kernelBinHandle_ == nullptr || kernelBinSize_ == 0) { |
| @@ -242,22 +253,20 @@ int32_t KernelInfoCollector::DumpHostKernelBin(const std::string &dumpPath) cons | |||
| 242 | IDE_LOGE("Kernel bin data is empty."); | 253 | IDE_LOGE("Kernel bin data is empty."); |
| 243 | return ADUMP_FAILED; | 254 | return ADUMP_FAILED; |
| 244 | } | 255 | } |
| 245 | - std::string kernelName = GetProcessedKernelName(); | 256 | + std::string hostKernelBinPath = GetHostOFilePath(dumpPath); |
| 246 | - Path hostKernelBinPath(dumpPath); | ||
| 247 | - std::string hostKernelBinName = StrUtils::Replace(kernelName, INVALID_FILE_NAME_CHAR, '_') + "_host.o"; | ||
| 248 | - hostKernelBinPath.Concat(hostKernelBinName); | ||
| 249 | // 幂等:_host.o 可能已由调用方在符号化前提前同步落盘。仅当磁盘上文件大小与待写入一致时才跳过, | 257 | // 幂等:_host.o 可能已由调用方在符号化前提前同步落盘。仅当磁盘上文件大小与待写入一致时才跳过, |
| 250 | // 避免上次部分写(如磁盘满)残留的截断/空文件被 Exist() 误判为已完成而永久跳过重写。 | 258 | // 避免上次部分写(如磁盘满)残留的截断/空文件被 Exist() 误判为已完成而永久跳过重写。 |
| 251 | struct stat hostBinStat; | 259 | struct stat hostBinStat; |
| 252 | - if (hostKernelBinPath.Exist() && stat(hostKernelBinPath.GetCString(), &hostBinStat) == 0 && | 260 | + if (stat(hostKernelBinPath.c_str(), &hostBinStat) == 0 && |
| 253 | static_cast<uint64_t>(hostBinStat.st_size) == static_cast<uint64_t>(kernelBinSize_)) { | 261 | static_cast<uint64_t>(hostBinStat.st_size) == static_cast<uint64_t>(kernelBinSize_)) { |
| 254 | - IDE_LOGI("[Dump][Exception] host kernel bin already dumped, skip. file: %s", hostKernelBinPath.GetCString()); | 262 | + IDE_LOGI("[Dump][Exception] host kernel bin already dumped, skip. file: %s", hostKernelBinPath.c_str()); |
| 263 | + outHostOPath = hostKernelBinPath; | ||
| 255 | return ADUMP_SUCCESS; | 264 | return ADUMP_SUCCESS; |
| 256 | } | 265 | } |
| 257 | - File hostKernelBinFile(hostKernelBinPath.GetString(), M_WRONLY | M_CREAT | M_TRUNC); | 266 | + File hostKernelBinFile(hostKernelBinPath, M_WRONLY | M_CREAT | M_TRUNC); |
| 258 | int32_t ret = hostKernelBinFile.IsFileOpen(); | 267 | int32_t ret = hostKernelBinFile.IsFileOpen(); |
| 259 | if (ret != ADUMP_SUCCESS) { | 268 | if (ret != ADUMP_SUCCESS) { |
| 260 | - IDE_LOGE("Open host kernel bin file[%s] failed.", hostKernelBinPath.GetCString()); | 269 | + IDE_LOGE("Open host kernel bin file[%s] failed.", hostKernelBinPath.c_str()); |
| 261 | return ADUMP_FAILED; | 270 | return ADUMP_FAILED; |
| 262 | } | 271 | } |
| 263 | static std::mutex KernelBinLock; | 272 | static std::mutex KernelBinLock; |
| @@ -267,21 +276,22 @@ int32_t KernelInfoCollector::DumpHostKernelBin(const std::string &dumpPath) cons | |||
| 267 | // 不清理残留文件(与拆分前行为一致):M_TRUNC 已把文件截为 0,失败后盘上会留下空/截断的 _host.o。 | 276 | // 不清理残留文件(与拆分前行为一致):M_TRUNC 已把文件截为 0,失败后盘上会留下空/截断的 _host.o。 |
| 268 | // 下次调用因大小不匹配会重写,不会误判为完整;但符号化阶段若在此期间读取会拿到残缺文件,故打印告警提醒。 | 277 | // 下次调用因大小不匹配会重写,不会误判为完整;但符号化阶段若在此期间读取会拿到残缺文件,故打印告警提醒。 |
| 269 | IDE_LOGE("Write host kernel bin file[%s] failed, an incomplete _host.o may remain on disk.", | 278 | IDE_LOGE("Write host kernel bin file[%s] failed, an incomplete _host.o may remain on disk.", |
| 270 | - hostKernelBinPath.GetCString()); | 279 | + hostKernelBinPath.c_str()); |
| 271 | return ADUMP_FAILED; | 280 | return ADUMP_FAILED; |
| 272 | } | 281 | } |
| 273 | // File::Write 内部循环处理短写、返回的是最后一次 mmWrite 的长度而非累计值,不能用它判完整性; | 282 | // File::Write 内部循环处理短写、返回的是最后一次 mmWrite 的长度而非累计值,不能用它判完整性; |
| 274 | // 改为写后 stat 校验最终文件大小,截断/短写(如磁盘满)时文件大小不足即判失败,避免残留截断文件被幂等误判为完整。 | 283 | // 改为写后 stat 校验最终文件大小,截断/短写(如磁盘满)时文件大小不足即判失败,避免残留截断文件被幂等误判为完整。 |
| 275 | - if (stat(hostKernelBinPath.GetCString(), &hostBinStat) != 0 || | 284 | + if (stat(hostKernelBinPath.c_str(), &hostBinStat) != 0 || |
| 276 | static_cast<uint64_t>(hostBinStat.st_size) != static_cast<uint64_t>(kernelBinSize_)) { | 285 | static_cast<uint64_t>(hostBinStat.st_size) != static_cast<uint64_t>(kernelBinSize_)) { |
| 277 | // 同上:残留的截断文件不主动删除,靠下次大小校验重写;此处打印告警提醒盘上存在不完整的 _host.o。 | 286 | // 同上:残留的截断文件不主动删除,靠下次大小校验重写;此处打印告警提醒盘上存在不完整的 _host.o。 |
| 278 | IDE_LOGE("Write host kernel bin file[%s] incomplete, fileSize=%lld, expect=%u, " | 287 | IDE_LOGE("Write host kernel bin file[%s] incomplete, fileSize=%lld, expect=%u, " |
| 279 | "an incomplete _host.o may remain on disk.", | 288 | "an incomplete _host.o may remain on disk.", |
| 280 | - hostKernelBinPath.GetCString(), static_cast<long long>(hostBinStat.st_size), kernelBinSize_); | 289 | + hostKernelBinPath.c_str(), static_cast<long long>(hostBinStat.st_size), kernelBinSize_); |
| 281 | return ADUMP_FAILED; | 290 | return ADUMP_FAILED; |
| 282 | } | 291 | } |
| 283 | - IDE_LOGE("[Dump][Exception] dump host kernel to file, file: %s", hostKernelBinPath.GetCString()); | 292 | + IDE_LOGE("[Dump][Exception] dump host kernel to file, file: %s", hostKernelBinPath.c_str()); |
| 284 | - (void)mmChmod(hostKernelBinPath.GetCString(), M_IRUSR); // 安全要求,落盘文件置为最小权限:用户只读, 400 | 293 | + (void)mmChmod(hostKernelBinPath.c_str(), M_IRUSR); // 安全要求,落盘文件置为最小权限:用户只读, 400 |
| 294 | + outHostOPath = hostKernelBinPath; | ||
| 285 | 295 | ||
| 286 | return ADUMP_SUCCESS; | 296 | return ADUMP_SUCCESS; |
| 287 | } | 297 | } |
| @@ -28,8 +28,11 @@ public: | |||
| 28 | int32_t InitFromBinHandle(rtBinHandle BinHandle, const std::string &kernelName); | 28 | int32_t InitFromBinHandle(rtBinHandle BinHandle, const std::string &kernelName); |
| 29 | int32_t LoadKernelBinBuffer(); | 29 | int32_t LoadKernelBinBuffer(); |
| 30 | int32_t StartCollectKernel(const std::string &dumpPath) const; | 30 | int32_t StartCollectKernel(const std::string &dumpPath) const; |
| 31 | - // 快速同步落 _host.o(纯内存 buffer 写),从 StartCollectKernel 拆出、供调用方单独同步调用。 | 31 | + // 快速同步落 _host.o(纯内存 buffer 写),从 StartCollectKernel 拆出、供调用方单独同步调用; |
| 32 | - int32_t DumpHostKernelBin(const std::string &dumpPath) const; | 32 | + // outHostOPath 回传落盘绝对路径,供后续 symbolize 使用。 |
| 33 | + int32_t DumpHostKernelBin(const std::string &dumpPath, std::string &outHostOPath) const; | ||
| 34 | + // 拼出 _host.o 落盘路径(不落盘),供回退/校验使用。 | ||
| 35 | + std::string GetHostOFilePath(const std::string &dumpPath) const; | ||
| 33 | std::string GetProcessedKernelName() const; | 36 | std::string GetProcessedKernelName() const; |
| 34 | std::vector<std::string> GetSearchPath() const; | 37 | std::vector<std::string> GetSearchPath() const; |
| 35 | std::string SearchJsonFiles(const std::string &rootPath, const std::string &targetString) const; | 38 | std::string SearchJsonFiles(const std::string &rootPath, const std::string &targetString) const; |
| @@ -369,7 +369,7 @@ void InitV200CubeAndL1PcFixGroups(std::vector<std::vector<PcFixGroup>>& table) | |||
| 369 | std::string BuildErrorRegistersStr( | 369 | std::string BuildErrorRegistersStr( |
| 370 | const char* const regNames[], size_t regNameNum, const uint32_t errReg[], size_t errRegLen) | 370 | const char* const regNames[], size_t regNameNum, const uint32_t errReg[], size_t errRegLen) |
| 371 | { | 371 | { |
| 372 | - constexpr size_t REG_ITEM_BUF_LEN = 128U; | 372 | + constexpr size_t REG_ITEM_BUF_LEN = 256U; |
| 373 | std::string result; | 373 | std::string result; |
| 374 | for (size_t i = 0; i < regNameNum && i < errRegLen; i++) { | 374 | for (size_t i = 0; i < regNameNum && i < errRegLen; i++) { |
| 375 | char item[REG_ITEM_BUF_LEN] = {0}; | 375 | char item[REG_ITEM_BUF_LEN] = {0}; |
| @@ -0,0 +1,591 @@ | |||
| 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 | +#include "kernel_source_symbolizer.h" | ||
| 11 | + | ||
| 12 | +#include <cerrno> | ||
| 13 | +#include <csignal> | ||
| 14 | +#include <cstdint> | ||
| 15 | +#include <cstdio> | ||
| 16 | +#include <cstdlib> | ||
| 17 | +#include <cstring> | ||
| 18 | +#include <ctime> | ||
| 19 | +#include <elf.h> | ||
| 20 | +#include <fcntl.h> | ||
| 21 | +#include <mutex> | ||
| 22 | +#include <poll.h> | ||
| 23 | +#include <spawn.h> | ||
| 24 | +#include <sstream> | ||
| 25 | +#include <sys/wait.h> | ||
| 26 | +#include <unistd.h> | ||
| 27 | +#include <vector> | ||
| 28 | + | ||
| 29 | +#include "securec.h" | ||
| 30 | +#include "mmpa_api.h" | ||
| 31 | +#include "path.h" | ||
| 32 | +#include "lib_path.h" | ||
| 33 | +#include "sys_utils.h" | ||
| 34 | +#include "log/adx_log.h" | ||
| 35 | +#include "log/hdc_log.h" | ||
| 36 | + | ||
| 37 | +// environ 由 <unistd.h> 声明(posix_spawn 需要当前进程环境变量表),无需再以 extern 方式引用外部变量。 | ||
| 38 | + | ||
| 39 | +namespace Adx { | ||
| 40 | +namespace { | ||
| 41 | +// 异常现场,宁可快速降级也不阻塞落盘:单次子进程解析超时 3 秒。 | ||
| 42 | +constexpr int64_t SYMBOLIZER_TIMEOUT_MS = 3000; | ||
| 43 | +// 超时回收:SIGTERM 后给子进程的自行退出宽限期,到期再 SIGKILL。 | ||
| 44 | +constexpr int64_t SYMBOLIZER_TERM_GRACE_MS = 200; | ||
| 45 | +// 宽限期内轮询 waitpid(WNOHANG) 的睡眠间隔(10ms)。 | ||
| 46 | +constexpr int64_t SYMBOLIZER_TERM_POLL_NS = 10 * 1000 * 1000; | ||
| 47 | +constexpr size_t READ_BUF_SIZE = 4096; | ||
| 48 | +constexpr char ENV_SYMBOLIZER[] = "ADUMP_LLVM_SYMBOLIZER"; | ||
| 49 | +// CANN 安装路径 + 架构目录下 llvm-symbolizer 的相对路径,如 <install>/x86_64-linux/bin/llvm-symbolizer。 | ||
| 50 | +constexpr char CANN_SYMBOLIZER_REL[] = "/bin/llvm-symbolizer"; | ||
| 51 | +// 系统默认安装位置回退。 | ||
| 52 | +constexpr char SYSTEM_SYMBOLIZER[] = "/usr/bin/llvm-symbolizer"; | ||
| 53 | +constexpr char UNKNOWN_MARK[] = "??"; | ||
| 54 | + | ||
| 55 | +int64_t NowMs() | ||
| 56 | +{ | ||
| 57 | + struct timespec ts{}; | ||
| 58 | + (void)clock_gettime(CLOCK_MONOTONIC, &ts); | ||
| 59 | + return static_cast<int64_t>(ts.tv_sec) * 1000 + ts.tv_nsec / 1000000; | ||
| 60 | +} | ||
| 61 | + | ||
| 62 | +// 校验路径存在且可执行。 | ||
| 63 | +bool IsExecutable(const std::string &path) | ||
| 64 | +{ | ||
| 65 | + if (path.empty()) { | ||
| 66 | + return false; | ||
| 67 | + } | ||
| 68 | + return access(path.c_str(), X_OK) == 0; | ||
| 69 | +} | ||
| 70 | + | ||
| 71 | +std::string GetEnvValue(const char *name) | ||
| 72 | +{ | ||
| 73 | + // mmGetEnv 内部对环境变量表的读取做了加锁保护,避免直接调用 getenv 的竞争条件。 | ||
| 74 | + char value[MMPA_MAX_PATH] = {0}; | ||
| 75 | + if (mmGetEnv(name, value, sizeof(value)) != EN_OK) { | ||
| 76 | + return std::string(); | ||
| 77 | + } | ||
| 78 | + return SysUtils::HandleEnv(value); | ||
| 79 | +} | ||
| 80 | + | ||
| 81 | +// 校验 ELF64 头合法性并定位 section 名字符串表;成功时回填 strTab/strTabSize。 | ||
| 82 | +// 以减法/除法比较,避免 e_shoff + e_shnum * sizeof(Shdr) 等加法回绕绕过越界校验。 | ||
| 83 | +bool LocateElfStrTab(const char *data, size_t size, Elf64_Ehdr &ehdr, const char *&strTab, size_t &strTabSize) | ||
| 84 | +{ | ||
| 85 | + if (memcpy_s(&ehdr, sizeof(ehdr), data, sizeof(ehdr)) != EOK) { | ||
| 86 | + return false; | ||
| 87 | + } | ||
| 88 | + if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0 || ehdr.e_ident[EI_CLASS] != ELFCLASS64) { | ||
| 89 | + return false; | ||
| 90 | + } | ||
| 91 | + if (ehdr.e_shentsize != sizeof(Elf64_Shdr) || ehdr.e_shnum == 0 || ehdr.e_shstrndx >= ehdr.e_shnum) { | ||
| 92 | + return false; | ||
| 93 | + } | ||
| 94 | + if (ehdr.e_shoff == 0 || ehdr.e_shoff >= size || | ||
| 95 | + (size - ehdr.e_shoff) / sizeof(Elf64_Shdr) < static_cast<uint64_t>(ehdr.e_shnum)) { | ||
| 96 | + return false; | ||
| 97 | + } | ||
级别:一般 问题:ElfHasSection 中 shTableEnd = e_shoff + e_shnumsizeof(Elf64_Shdr) 可能整数溢出绕过边界检查 影响:恶意或损坏的 ELF 中 e_shoff 接近 UINT64_MAX 时 shTableEnd 回绕为小值,绕过 shTableEnd > size 守卫,后续 memcpy_s 从 data+off 越界读取 修复建议:在计算前单独检查 e_shoff < size 且 e_shoff <= size - ehdr.e_shnumsizeof(Elf64_Shdr) ![]() ![]() sinat_31531339 Aug 11 Comments: Aug 11 Comments: ![]() ![]() | |||
| 98 | + Elf64_Shdr strShdr{}; | ||
| 99 | + const size_t strShdrOff = static_cast<size_t>(ehdr.e_shoff) + | ||
| 100 | + static_cast<size_t>(ehdr.e_shstrndx) * sizeof(Elf64_Shdr); | ||
| @@ -0,0 +1,58 @@ | |||
| 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 | +#ifndef KERNEL_SOURCE_SYMBOLIZER_H | ||
| 11 | +#define KERNEL_SOURCE_SYMBOLIZER_H | ||
| 12 | + | ||
| 13 | +#include <cstdint> | ||
| 14 | +#include <string> | ||
| 15 | +#include <vector> | ||
| 16 | + | ||
| 17 | +namespace Adx { | ||
| 18 | + | ||
| 19 | +// 单个 PC 偏移经 llvm-symbolizer 解析后的源码信息(仅取源码位置,不解析函数名)。 | ||
| 20 | +struct SymbolizeResult { | ||
| 21 | + bool ok = false; | ||
| 22 | + std::string srcFile; // 源文件路径 | ||
| 23 | + uint32_t srcLine = 0; | ||
| 24 | + uint32_t srcColumn = 0; | ||
| 25 | +}; | ||
| 26 | + | ||
| 27 | +// 借助 llvm-symbolizer 将 .o 内的 PC 偏移解析为源码行号(不依赖 -f/-C/-i 等参数约束输出格式)。 | ||
| 28 | +// 所有接口均为 best-effort:定位失败/超时/解析失败只告警并返回 false,绝不阻断落盘。 | ||
| 29 | +class KernelSourceSymbolizer { | ||
| 30 | +public: | ||
| 31 | + // 对同一个 .o 一次性解析多个偏移,results 与 offsets 一一对应。 | ||
| 32 | + static bool Symbolize(const std::string &oFilePath, const std::vector<uint64_t> &offsets, | ||
| 33 | + std::vector<SymbolizeResult> &results); | ||
| 34 | + | ||
| 35 | + // llvm-symbolizer 是否可用(定位结果带缓存)。 | ||
| 36 | + static bool IsAvailable(); | ||
| 37 | + | ||
| 38 | + // 探测 ELF 中是否存在 .debug_line 段,用于决定是否回退到 kernel_meta 的 .o。 | ||
| 39 | + static bool HasDebugLine(const std::string &oFilePath); | ||
| 40 | + | ||
| 41 | +#ifdef __ADUMP_LLT | ||
| 42 | + // 测试专用:清除工具定位缓存,保证用例间相互独立。 | ||
| 43 | + static void ResetLocateCacheForTest(); | ||
| 44 | +#endif | ||
| 45 | + | ||
| 46 | +private: | ||
| 47 | + // 定位可执行文件:环境变量 ADUMP_LLVM_SYMBOLIZER 优先,其次 ASCEND 工具链相对路径。 | ||
| 48 | + static const std::string &LocateSymbolizer(); | ||
| 49 | + | ||
| 50 | + // posix_spawn + 超时执行,无 shell。inputLines 为逐行 stdin 内容, | ||
| 51 | + // results 按输出块顺序回填(大小由调用方预置为有效请求数)。 | ||
| 52 | + static bool RunSymbolizer(const std::string &tool, const std::string &inputLines, | ||
| 53 | + std::vector<SymbolizeResult> &results); | ||
| 54 | +}; | ||
| 55 | + | ||
| 56 | +} // namespace Adx | ||
| 57 | + | ||
| 58 | +#endif // KERNEL_SOURCE_SYMBOLIZER_H | ||
| @@ -12,9 +12,12 @@ | |||
| 12 | #include <elf.h> | 12 | #include <elf.h> |
| 13 | #include <limits> | 13 | #include <limits> |
| 14 | #include <mutex> | 14 | #include <mutex> |
| 15 | +#include <sstream> | ||
| 15 | #include "securec.h" | 16 | #include "securec.h" |
| 16 | #include "runtime/kernel.h" | 17 | #include "runtime/kernel.h" |
| 17 | #include "kernel_symbol_locator.h" | 18 | #include "kernel_symbol_locator.h" |
| 19 | +#include "kernel_info_collector.h" | ||
| 20 | +#include "kernel_source_symbolizer.h" | ||
| 18 | #include "exception_info_common.h" | 21 | #include "exception_info_common.h" |
| 19 | #include "log/adx_log.h" | 22 | #include "log/adx_log.h" |
| 20 | #include "log/hdc_log.h" | 23 | #include "log/hdc_log.h" |
| @@ -454,6 +457,69 @@ bool ParseSymbolTables( | |||
| 454 | } | 457 | } |
| 455 | return true; | 458 | return true; |
| 456 | } | 459 | } |
| 460 | + | ||
| 461 | +// 分类汇总用:(oFilePath, fixedPCOffset) 相同的多个异常 core 归为一组。 | ||
| 462 | +struct SummaryGroup { | ||
| 463 | + std::string oFilePath; | ||
| 464 | + uint64_t fixedPCOffset = 0; | ||
| 465 | + bool hasSymbol = false; | ||
| 466 | + std::string symbolName; | ||
| 467 | + uint64_t symbolOffset = 0; | ||
| 468 | + SymbolizeResult src; | ||
| 469 | + std::vector<const ErrorLocation*> cores; | ||
| 470 | +}; | ||
| 471 | + | ||
| 472 | +// 按 (oFilePath, fixedPCOffset) 聚类:命中已有组则追加 core,否则新建组并拷贝定位信息。 | ||
| 473 | +std::vector<SummaryGroup> BuildSummaryGroups(const std::vector<ErrorLocation>& locations) | ||
| 474 | +{ | ||
| 475 | + std::vector<SummaryGroup> groups; | ||
| 476 | + for (const ErrorLocation& loc : locations) { | ||
| 477 | + SummaryGroup* target = nullptr; | ||
| 478 | + for (SummaryGroup& g : groups) { | ||
| 479 | + if (g.oFilePath == loc.oFilePath && g.fixedPCOffset == loc.fixedPCOffset) { | ||
| 480 | + target = &g; | ||
| 481 | + break; | ||
| 482 | + } | ||
| 483 | + } | ||
| 484 | + if (target == nullptr) { | ||
| 485 | + groups.emplace_back(); | ||
| 486 | + target = &groups.back(); | ||
| 487 | + target->oFilePath = loc.oFilePath; | ||
| 488 | + target->fixedPCOffset = loc.fixedPCOffset; | ||
| 489 | + target->hasSymbol = loc.hasSymbol; | ||
| 490 | + target->symbolName = loc.symbolName; | ||
| 491 | + target->symbolOffset = loc.symbolOffset; | ||
| 492 | + target->src = loc.src; | ||
| 493 | + } | ||
| 494 | + target->cores.push_back(&loc); | ||
| 495 | + } | ||
| 496 | + return groups; | ||
| 497 | +} | ||
| 498 | + | ||
| 499 | +// 打印单个分组:core 列表、symbol+偏移、func、source 行号,缺失信息统一显示 unknown。 | ||
| 500 | +void PrintSummaryGroup(size_t index, const SummaryGroup& g) | ||
| 501 | +{ | ||
| 502 | + std::ostringstream coresOss; | ||
| 503 | + for (size_t c = 0; c < g.cores.size(); ++c) { | ||
| 504 | + if (c != 0) { | ||
| 505 | + coresOss << ","; | ||
| 506 | + } | ||
| 507 | + coresOss << "{id=" << g.cores[c]->coreId << ",type=" << g.cores[c]->coreType << "}"; | ||
| 508 | + } | ||
| 509 | + std::ostringstream symbolOss; | ||
| 510 | + if (g.hasSymbol) { | ||
| 511 | + symbolOss << g.symbolName << "+0x" << std::hex << g.symbolOffset; | ||
| 512 | + } else { | ||
| 513 | + symbolOss << "unknown"; | ||
| 514 | + } | ||
| 515 | + const std::string sourceStr = g.src.ok | ||
| 516 | + ? (g.src.srcFile + ":" + std::to_string(g.src.srcLine) + ":" + std::to_string(g.src.srcColumn)) | ||
| 517 | + : "unknown"; | ||
| 518 | + IDE_LOGE("[Dump][Exception][Symbolize] Group[%zu] oFile=%s fixedPCOffset=0x%lx symbol=%s " | ||
| 519 | + "source=%s cores=[%s]", | ||
| 520 | + index, g.oFilePath.empty() ? "unknown" : g.oFilePath.c_str(), g.fixedPCOffset, | ||
| 521 | + symbolOss.str().c_str(), sourceStr.c_str(), coresOss.str().c_str()); | ||
| 522 | +} | ||
| 457 | } // namespace | 523 | } // namespace |
| 458 | 524 | ||
| 459 | std::unordered_map<rtBinHandle, KernelSymbolSet> KernelSymbolLocator::cache_; | 525 | std::unordered_map<rtBinHandle, KernelSymbolSet> KernelSymbolLocator::cache_; |
| @@ -473,6 +539,12 @@ void KernelSymbolLocator::ResetState() | |||
| 473 | kernelDeviceStartPC_ = 0; | 539 | kernelDeviceStartPC_ = 0; |
| 474 | hasKernelDeviceStartPC_ = false; | 540 | hasKernelDeviceStartPC_ = false; |
| 475 | initialized_ = false; | 541 | initialized_ = false; |
| 542 | + oFilePath_.clear(); | ||
| 543 | +} | ||
| 544 | + | ||
| 545 | +void KernelSymbolLocator::SetOFilePath(const std::string& oFilePath) | ||
| 546 | +{ | ||
| 547 | + oFilePath_ = oFilePath; | ||
| 476 | } | 548 | } |
| 477 | 549 | ||
| 478 | void KernelSymbolLocator::UpdateStartPCFromDeviceAddr(rtBinHandle binHandle) | 550 | void KernelSymbolLocator::UpdateStartPCFromDeviceAddr(rtBinHandle binHandle) |
| @@ -576,8 +648,15 @@ bool KernelSymbolLocator::GetCorrectedStartPC(const rtExceptionErrRegInfo_t& cor | |||
| 576 | return false; | 648 | return false; |
| 577 | } | 649 | } |
| 578 | 650 | ||
| 579 | -void KernelSymbolLocator::PrintErrorForCore(rtExceptionErrRegInfo_t coreInfo) | 651 | +void KernelSymbolLocator::PrintErrorForCore(rtExceptionErrRegInfo_t coreInfo, ErrorLocation& outLocation) |
| 580 | { | 652 | { |
| 653 | + outLocation.coreId = coreInfo.coreId; | ||
| 654 | + outLocation.coreType = static_cast<uint32_t>(coreInfo.coreType); | ||
| 655 | + outLocation.oFilePath = oFilePath_; | ||
🟡 Medium Priority changed line: 受影响行为: 失败模式:假设调用方先处理 core A(命中符号, 当前调用方( 建议:在 PrintErrorForCore 开头(第 592 行之后)显式重置 hasSymbol 和 skipped,确保所有输出路径都完整初始化这两个标志位。 ![]() ![]() Incorrect? | |||
| 656 | + // hasSymbol/skipped 只在特定分支置 true,入口先复位,避免调用方复用同一 ErrorLocation 时残留脏值。 | ||
| 657 | + outLocation.hasSymbol = false; | ||
| 658 | + outLocation.skipped = false; | ||
| 659 | + | ||
| 581 | uint32_t coreType = static_cast<uint32_t>(coreInfo.coreType); | 660 | uint32_t coreType = static_cast<uint32_t>(coreInfo.coreType); |
| 582 | IDE_LOGE("[Dump][Exception] Error register information. coreId=%u, coreType=%u, %s", | 661 | IDE_LOGE("[Dump][Exception] Error register information. coreId=%u, coreType=%u, %s", |
| 583 | coreInfo.coreId, coreType, GetErrorRegisters(coreInfo).c_str()); | 662 | coreInfo.coreId, coreType, GetErrorRegisters(coreInfo).c_str()); |
| @@ -587,25 +666,38 @@ void KernelSymbolLocator::PrintErrorForCore(rtExceptionErrRegInfo_t coreInfo) | |||
| 587 | IDE_LOGI("Correct startPC by kernel address. coreId=%u, coreType=%u, originalStartPC=0x%lx, " | 666 | IDE_LOGI("Correct startPC by kernel address. coreId=%u, coreType=%u, originalStartPC=0x%lx, " |
| 588 | "fixedStartPC=0x%lx.", coreInfo.coreId, coreType, coreInfo.startPC, fixedStartPC); | 667 | "fixedStartPC=0x%lx.", coreInfo.coreId, coreType, coreInfo.startPC, fixedStartPC); |
| 589 | } | 668 | } |
| 590 | - | ||
| 591 | if (fixedCurrentPC < fixedStartPC) { | 669 | if (fixedCurrentPC < fixedStartPC) { |
| 592 | IDE_LOGE("coreId=%u, coreType=%u, fixedCurrentPC=0x%lx < fixedStartPC=0x%lx, " | 670 | IDE_LOGE("coreId=%u, coreType=%u, fixedCurrentPC=0x%lx < fixedStartPC=0x%lx, " |
| 593 | "originalCurrentPC=0x%lx, originalStartPC=0x%lx, skip lookup symbol.", | 671 | "originalCurrentPC=0x%lx, originalStartPC=0x%lx, skip lookup symbol.", |
| 594 | coreInfo.coreId, coreType, fixedCurrentPC, fixedStartPC, coreInfo.currentPC, coreInfo.startPC); | 672 | coreInfo.coreId, coreType, fixedCurrentPC, fixedStartPC, coreInfo.currentPC, coreInfo.startPC); |
| 673 | + outLocation.skipped = true; | ||
| 595 | return; | 674 | return; |
| 596 | } | 675 | } |
| 597 | 676 | ||
| 598 | const uint64_t fixedPCOffset = fixedCurrentPC - fixedStartPC; | 677 | const uint64_t fixedPCOffset = fixedCurrentPC - fixedStartPC; |
| 678 | + outLocation.fixedPCOffset = fixedPCOffset; | ||
| 599 | IDE_LOGE("[Dump][Exception] Error PC information. coreId=%u, coreType=%u, originalStartPC=0x%lx, " | 679 | IDE_LOGE("[Dump][Exception] Error PC information. coreId=%u, coreType=%u, originalStartPC=0x%lx, " |
| 600 | "fixedStartPC=0x%lx, originalCurrentPC=0x%lx, fixedCurrentPC=0x%lx, fixedPCOffset=0x%lx.", | 680 | "fixedStartPC=0x%lx, originalCurrentPC=0x%lx, fixedCurrentPC=0x%lx, fixedPCOffset=0x%lx.", |
| 601 | coreInfo.coreId, coreType, coreInfo.startPC, fixedStartPC, coreInfo.currentPC, fixedCurrentPC, | 681 | coreInfo.coreId, coreType, coreInfo.startPC, fixedStartPC, coreInfo.currentPC, fixedCurrentPC, |
| 602 | fixedPCOffset); | 682 | fixedPCOffset); |
| 603 | 683 | ||
| 684 | + // 源码解析不在此逐核进行:偏移已回填 outLocation.fixedPCOffset,由 SymbolizeCollectedLocations | ||
| 685 | + // 收齐所有核后对同一 .o 一次性批量 symbolize,避免每核各起一个 llvm-symbolizer 进程放大超时。 | ||
| 686 | + MatchSymbolForCore(coreInfo, fixedPCOffset, outLocation); | ||
| 687 | +} | ||
| 688 | + | ||
| 689 | +void KernelSymbolLocator::MatchSymbolForCore(const rtExceptionErrRegInfo_t& coreInfo, uint64_t fixedPCOffset, | ||
| 690 | + ErrorLocation& outLocation) | ||
| 691 | +{ | ||
| 692 | + const uint32_t coreType = static_cast<uint32_t>(coreInfo.coreType); | ||
| 604 | const KernelSymbol* matchedSymbol = FindBestMatchedSymbol(kernelSymbols_.symbols, fixedPCOffset); | 693 | const KernelSymbol* matchedSymbol = FindBestMatchedSymbol(kernelSymbols_.symbols, fixedPCOffset); |
| 605 | if (matchedSymbol != nullptr) { | 694 | if (matchedSymbol != nullptr) { |
| 695 | + outLocation.hasSymbol = true; | ||
| 696 | + outLocation.symbolName = matchedSymbol->name; | ||
| 697 | + outLocation.symbolOffset = fixedPCOffset - matchedSymbol->offset; | ||
| 606 | IDE_LOGE("[Dump][Exception] Error symbol information. coreId=%u, coreType=%u, " | 698 | IDE_LOGE("[Dump][Exception] Error symbol information. coreId=%u, coreType=%u, " |
| 607 | "symbol=%s+0x%lx.", coreInfo.coreId, coreType, matchedSymbol->name.c_str(), | 699 | "symbol=%s+0x%lx.", coreInfo.coreId, coreType, matchedSymbol->name.c_str(), |
| 608 | - fixedPCOffset - matchedSymbol->offset); | 700 | + outLocation.symbolOffset); |
| 609 | return; | 701 | return; |
| 610 | } | 702 | } |
| 611 | 703 | ||
| @@ -618,7 +710,37 @@ void KernelSymbolLocator::PrintErrorForCore(rtExceptionErrRegInfo_t coreInfo) | |||
| 618 | static_cast<uint32_t>(hasSymbolRange), minSymbolOffset, maxSymbolEnd); | 710 | static_cast<uint32_t>(hasSymbolRange), minSymbolOffset, maxSymbolEnd); |
| 619 | } | 711 | } |
| 620 | 712 | ||
| 621 | -int32_t KernelSymbolLocator::LocateAndPrintErrorSymbols(const ExceptionRegInfo& exceptionRegInfo) | 713 | +void KernelSymbolLocator::SymbolizeCollectedLocations(std::vector<ErrorLocation>& locations) const |
| 714 | +{ | ||
| 715 | + if (oFilePath_.empty() || !KernelSourceSymbolizer::IsAvailable()) { | ||
| 716 | + return; | ||
| 717 | + } | ||
| 718 | + // 收集所有未跳过 core 的偏移;idxMap 记录第 k 个偏移对应 locations 中的下标,便于按序回填。 | ||
| 719 | + std::vector<uint64_t> offsets; | ||
| 720 | + std::vector<size_t> idxMap; | ||
| 721 | + for (size_t i = 0; i < locations.size(); ++i) { | ||
| 722 | + if (!locations[i].skipped) { | ||
| 723 | + offsets.push_back(locations[i].fixedPCOffset); | ||
| 724 | + idxMap.push_back(i); | ||
| 725 | + } | ||
| 726 | + } | ||
| 727 | + if (offsets.empty()) { | ||
| 728 | + return; | ||
| 729 | + } | ||
| 730 | + // 同一 .o 的全部偏移由一个 llvm-symbolizer 进程一次解析,最坏耗时收敛为单次超时。 | ||
| 731 | + std::vector<SymbolizeResult> results; | ||
| 732 | + if (!KernelSourceSymbolizer::Symbolize(oFilePath_, offsets, results)) { | ||
| 733 | + IDE_LOGW("Symbolize kernel source failed for all cores, oFile=%s, offsetCount=%zu.", | ||
| 734 | + oFilePath_.c_str(), offsets.size()); | ||
| 735 | + return; | ||
| 736 | + } | ||
| 737 | + for (size_t k = 0; k < idxMap.size() && k < results.size(); ++k) { | ||
| 738 | + locations[idxMap[k]].src = results[k]; | ||
| 739 | + } | ||
| 740 | +} | ||
| 741 | + | ||
| 742 | +int32_t KernelSymbolLocator::LocateErrorSymbols(const ExceptionRegInfo& exceptionRegInfo, | ||
| 743 | + std::vector<ErrorLocation>& outLocations) | ||
| 622 | { | 744 | { |
| 623 | IDE_CTRL_VALUE_WARN(initialized_, return ADUMP_FAILED, "KernelSymbolLocator not initialized."); | 745 | IDE_CTRL_VALUE_WARN(initialized_, return ADUMP_FAILED, "KernelSymbolLocator not initialized."); |
| 624 | 746 | ||
| @@ -626,14 +748,18 @@ int32_t KernelSymbolLocator::LocateAndPrintErrorSymbols(const ExceptionRegInfo& | |||
| 626 | exceptionRegInfo.errRegInfo != nullptr && exceptionRegInfo.coreNum != 0, return ADUMP_FAILED, | 748 | exceptionRegInfo.errRegInfo != nullptr && exceptionRegInfo.coreNum != 0, return ADUMP_FAILED, |
| 627 | "Exception register info is null or core num is zero."); | 749 | "Exception register info is null or core num is zero."); |
| 628 | 750 | ||
| 751 | + // 先逐核定位偏移与符号,再对同一 .o 的所有偏移一次性批量 symbolize,避免逐核各起进程放大超时。 | ||
| 629 | for (uint32_t i = 0; i < exceptionRegInfo.coreNum; i++) { | 752 | for (uint32_t i = 0; i < exceptionRegInfo.coreNum; i++) { |
| 630 | - PrintErrorForCore(exceptionRegInfo.errRegInfo[i]); | 753 | + ErrorLocation loc; |
| 754 | + PrintErrorForCore(exceptionRegInfo.errRegInfo[i], loc); | ||
| 755 | + outLocations.push_back(loc); | ||
| 631 | } | 756 | } |
| 757 | + SymbolizeCollectedLocations(outLocations); | ||
| 632 | return ADUMP_SUCCESS; | 758 | return ADUMP_SUCCESS; |
| 633 | } | 759 | } |
| 634 | 760 | ||
| 635 | -int32_t KernelSymbolLocator::LocateAndPrintErrorSymbolsForCore( | 761 | +int32_t KernelSymbolLocator::LocateErrorSymbolsForCore( |
| 636 | - uint32_t coreId, uint32_t coreType, ExceptionRegInfo exceptionRegInfo) | 762 | + uint32_t coreId, uint32_t coreType, ExceptionRegInfo exceptionRegInfo, ErrorLocation& outLocation) |
| 637 | { | 763 | { |
| 638 | IDE_CTRL_VALUE_WARN(initialized_, return ADUMP_FAILED, "KernelSymbolLocator not initialized."); | 764 | IDE_CTRL_VALUE_WARN(initialized_, return ADUMP_FAILED, "KernelSymbolLocator not initialized."); |
| 639 | 765 | ||
| @@ -652,10 +778,28 @@ int32_t KernelSymbolLocator::LocateAndPrintErrorSymbolsForCore( | |||
| 652 | IDE_CTRL_VALUE_WARN(coreInfo != nullptr, return ADUMP_FAILED, | 778 | IDE_CTRL_VALUE_WARN(coreInfo != nullptr, return ADUMP_FAILED, |
| 653 | "Core exception register info is not found, coreId=%u, coreType=%u.", coreId, coreType); | 779 | "Core exception register info is not found, coreId=%u, coreType=%u.", coreId, coreType); |
| 654 | 780 | ||
| 655 | - PrintErrorForCore(*coreInfo); | 781 | + // 先定位偏移与符号,再对该核偏移做一次 symbolize(单核路径每个 .o 本就是一次进程调用)。 |
| 782 | + PrintErrorForCore(*coreInfo, outLocation); | ||
| 783 | + std::vector<ErrorLocation> single{outLocation}; | ||
| 784 | + SymbolizeCollectedLocations(single); | ||
| 785 | + outLocation = single[0]; | ||
| 656 | return ADUMP_SUCCESS; | 786 | return ADUMP_SUCCESS; |
| 657 | } | 787 | } |
| 658 | 788 | ||
| 789 | +void KernelSymbolLocator::PrintClassificationSummary(const std::vector<ErrorLocation>& locations) | ||
| 790 | +{ | ||
| 791 | + if (locations.empty()) { | ||
| 792 | + return; | ||
| 793 | + } | ||
| 794 | + // 按 (oFilePath, fixedPCOffset) 聚类:同一 .o 同一偏移的多核归为一组。 | ||
| 795 | + const std::vector<SummaryGroup> groups = BuildSummaryGroups(locations); | ||
| 796 | + IDE_LOGE("[Dump][Exception][Symbolize] classification summary. cores=%zu, groups=%zu.", | ||
| 797 | + locations.size(), groups.size()); | ||
| 798 | + for (size_t i = 0; i < groups.size(); ++i) { | ||
| 799 | + PrintSummaryGroup(i, groups[i]); | ||
| 800 | + } | ||
| 801 | +} | ||
| 802 | + | ||
| 659 | uint64_t KernelSymbolLocator::FixPcByErrorRegs(const rtExceptionErrRegInfo_t& coreInfo) | 803 | uint64_t KernelSymbolLocator::FixPcByErrorRegs(const rtExceptionErrRegInfo_t& coreInfo) |
| 660 | { | 804 | { |
| 661 | PcFixerInterface* fixer = PcFixerFactory::GetInstance(); | 805 | PcFixerInterface* fixer = PcFixerFactory::GetInstance(); |
| @@ -674,7 +818,21 @@ std::string KernelSymbolLocator::GetErrorRegisters(const rtExceptionErrRegInfo_t | |||
| 674 | return fixer->GetErrorRegisters(coreInfo.errReg, RT_ERR_REG_NUMS); | 818 | return fixer->GetErrorRegisters(coreInfo.errReg, RT_ERR_REG_NUMS); |
| 675 | } | 819 | } |
| 676 | 820 | ||
| 677 | -void KernelSymbolLocator::DumpErrorSymbols(const rtExceptionInfo& exception, ExceptionRegInfo& exceptionRegInfo) | 821 | +std::string KernelSymbolLocator::ResolveOFilePath(const std::string& hostOPath) |
| 822 | +{ | ||
| 823 | + if (hostOPath.empty()) { | ||
| 824 | + return ""; | ||
| 825 | + } | ||
| 826 | + // _host.o 含 .debug_line 时直接使用;否则留空,由调用方决定是否回退到 kernel_meta 的 .o。 | ||
| 827 | + if (KernelSourceSymbolizer::HasDebugLine(hostOPath)) { | ||
| 828 | + return hostOPath; | ||
| 829 | + } | ||
| 830 | + IDE_LOGW("Host kernel .o has no .debug_line, source location unavailable, oFile=%s.", hostOPath.c_str()); | ||
| 831 | + return ""; | ||
| 832 | +} | ||
| 833 | + | ||
| 834 | +void KernelSymbolLocator::DumpErrorSymbols(const rtExceptionInfo& exception, ExceptionRegInfo& exceptionRegInfo, | ||
| 835 | + const std::string& dumpPath) | ||
| 678 | { | 836 | { |
| 679 | rtExceptionArgsInfo_t exceptionArgsInfo{}; | 837 | rtExceptionArgsInfo_t exceptionArgsInfo{}; |
| 680 | int32_t ret = ExceptionInfoCommon::GetExceptionInfo(exception, exceptionArgsInfo); | 838 | int32_t ret = ExceptionInfoCommon::GetExceptionInfo(exception, exceptionArgsInfo); |
| @@ -691,15 +849,33 @@ void KernelSymbolLocator::DumpErrorSymbols(const rtExceptionInfo& exception, Exc | |||
| 691 | IDE_CTRL_VALUE_FAILED(ret == ADUMP_SUCCESS, return, "Parse kernel symbols failed, skip dump error symbols."); | 849 | IDE_CTRL_VALUE_FAILED(ret == ADUMP_SUCCESS, return, "Parse kernel symbols failed, skip dump error symbols."); |
| 692 | locator.UpdateStartPCFromDeviceAddr(kernelInfo.bin); | 850 | locator.UpdateStartPCFromDeviceAddr(kernelInfo.bin); |
| 693 | 851 | ||
| 694 | - ret = locator.LocateAndPrintErrorSymbols(exceptionRegInfo); | 852 | + // _host.o 已由调用方(DumpHostKernelBinBeforeSymbolize / 回调路径)提前无条件落盘, |
| 853 | + // 此处只复用其路径决定实际 symbolize 用的 .o,不再重复落盘:避免把落盘可靠性绑定到 | ||
| 854 | + // 本函数前面的符号解析步骤(GetBinData/InitFromBinBuffer 失败时提前 return 会漏落盘)。 | ||
| 855 | + KernelInfoCollector collector; | ||
级别:提示 问题:DumpErrorSymbols 中 collector.LoadKernelInfo(exceptionArgsInfo) 返回值被忽略 影响:若 LoadKernelInfo 内部未设置 kernelName,GetHostOFilePath 返回非唯一的 "_host.o",可能在巧合存在该文件时误用 修复建议:检查返回值,失败时不设 oFilePath(留空让 SymbolizeLocations 跳过) ![]() ![]() sinat_31531339 Aug 11 Comments: Aug 11 Comments: ![]() ![]() | |||
| 856 | + collector.LoadKernelInfo(exceptionArgsInfo); | ||
| 857 | + std::string hostOPath = collector.GetHostOFilePath(dumpPath); | ||
| 858 | + // hostOPath 为空说明 kernelName 缺失/路径拼接失败,符号解析将无 .o 可用,仅告警不阻断后续流程。 | ||
| 859 | + if (hostOPath.empty()) { | ||
| 860 | + IDE_LOGW("Host .o path is empty, symbolize may fall back without source location."); | ||
| 861 | + } else { | ||
| 862 | + locator.SetOFilePath(ResolveOFilePath(hostOPath)); | ||
| 863 | + } | ||
| 864 | + | ||
| 865 | + std::vector<ErrorLocation> locations; | ||
| 866 | + ret = locator.LocateErrorSymbols(exceptionRegInfo, locations); | ||
| 695 | IDE_CTRL_VALUE_WARN(ret == ADUMP_SUCCESS, return, "Locate kernel error symbols failed, ret=%d.", ret); | 867 | IDE_CTRL_VALUE_WARN(ret == ADUMP_SUCCESS, return, "Locate kernel error symbols failed, ret=%d.", ret); |
| 868 | + // 未找到 llvm-symbolizer 时不打印聚类汇总(无源码信息时该汇总无增量价值)。 | ||
| 869 | + if (KernelSourceSymbolizer::IsAvailable()) { | ||
| 870 | + PrintClassificationSummary(locations); | ||
| 871 | + } | ||
| 696 | } | 872 | } |
| 697 | 873 | ||
| 698 | -void KernelSymbolLocator::DumpErrorSymbols(const rtExceptionInfo& exception) | 874 | +void KernelSymbolLocator::DumpErrorSymbols(const rtExceptionInfo& exception, const std::string& dumpPath) |
| 699 | { | 875 | { |
| 700 | ExceptionRegInfo exceptionRegInfo{0, nullptr}; | 876 | ExceptionRegInfo exceptionRegInfo{0, nullptr}; |
| 701 | if (ExceptionInfoCommon::GetExceptionRegInfo(exception, exceptionRegInfo) == ADUMP_SUCCESS) { | 877 | if (ExceptionInfoCommon::GetExceptionRegInfo(exception, exceptionRegInfo) == ADUMP_SUCCESS) { |
| 702 | - DumpErrorSymbols(exception, exceptionRegInfo); | 878 | + DumpErrorSymbols(exception, exceptionRegInfo, dumpPath); |
| 703 | } | 879 | } |
| 704 | } | 880 | } |
| 705 | 881 | ||
| @@ -17,6 +17,7 @@ | |||
| 17 | #include "runtime/rt.h" | 17 | #include "runtime/rt.h" |
| 18 | #include "exception_info_common.h" | 18 | #include "exception_info_common.h" |
| 19 | #include "kernel_pc_fixer.h" | 19 | #include "kernel_pc_fixer.h" |
| 20 | +#include "kernel_source_symbolizer.h" | ||
| 20 | #include "adump_pub.h" | 21 | #include "adump_pub.h" |
| 21 | 22 | ||
| 22 | namespace Adx { | 23 | namespace Adx { |
| @@ -35,6 +36,19 @@ struct KernelSymbolSet { | |||
| 35 | std::vector<KernelSymbol> symbols; | 36 | std::vector<KernelSymbol> symbols; |
| 36 | }; | 37 | }; |
| 37 | 38 | ||
| 39 | +// 单个异常 core 的定位结果:寄存器修正 PC、符号命中、源码行号,以及使用的 .o。 | ||
| 40 | +struct ErrorLocation { | ||
| 41 | + uint32_t coreId = 0; | ||
| 42 | + uint32_t coreType = 0; | ||
| 43 | + uint64_t fixedPCOffset = 0; | ||
| 44 | + bool hasSymbol = false; | ||
| 45 | + std::string symbolName; // FindBestMatchedSymbol 命中的函数符号名 | ||
| 46 | + uint64_t symbolOffset = 0; // fixedPCOffset - symbol.offset | ||
| 47 | + SymbolizeResult src; // llvm-symbolizer 解析出的源码信息 | ||
| 48 | + std::string oFilePath; // 实际用于 symbolize 的 .o | ||
| 49 | + bool skipped = false; // fixedCurrentPC < fixedStartPC 等无法定位的情形 | ||
| 50 | +}; | ||
| 51 | + | ||
| 38 | class KernelSymbolLocator { | 52 | class KernelSymbolLocator { |
| 39 | public: | 53 | public: |
| 40 | KernelSymbolLocator(); | 54 | KernelSymbolLocator(); |
| @@ -44,13 +58,27 @@ public: | |||
| 44 | int32_t InitFromBinBuffer(const std::string& binData); | 58 | int32_t InitFromBinBuffer(const std::string& binData); |
| 45 | void UpdateStartPCFromDeviceAddr(rtBinHandle binHandle); | 59 | void UpdateStartPCFromDeviceAddr(rtBinHandle binHandle); |
| 46 | 60 | ||
| 47 | - int32_t LocateAndPrintErrorSymbols(const ExceptionRegInfo& regInfo); | 61 | + // 注入实际用于 symbolize 的 .o 路径(_host.o 优先,无 .debug_line 时上层可回退)。 |
| 48 | - int32_t LocateAndPrintErrorSymbolsForCore(uint32_t coreId, uint32_t coreType, ExceptionRegInfo exceptionRegInfo); | 62 | + void SetOFilePath(const std::string& oFilePath); |
| 63 | + | ||
| 64 | + // 定位并打印所有异常 core 的错误符号,同时把每个 core 的定位结果收集到 outLocations。 | ||
| 65 | + // 每个 core 拿到偏移后即刻 symbolize 并回填 src(RunSymbolizer 原样打印原始输出)。 | ||
| 66 | + int32_t LocateErrorSymbols(const ExceptionRegInfo& regInfo, std::vector<ErrorLocation>& outLocations); | ||
| 67 | + // 构建指定 core 的 ErrorLocation(定位 + 打印错误寄存器 + 即刻 symbolize 回填 src)。 | ||
| 68 | + int32_t LocateErrorSymbolsForCore(uint32_t coreId, uint32_t coreType, ExceptionRegInfo exceptionRegInfo, | ||
| 69 | + ErrorLocation& outLocation); | ||
| 49 | 70 | ||
| 50 | static uint64_t FixPcByErrorRegs(const rtExceptionErrRegInfo& coreInfo); | 71 | static uint64_t FixPcByErrorRegs(const rtExceptionErrRegInfo& coreInfo); |
| 51 | static std::string GetErrorRegisters(const rtExceptionErrRegInfo& coreInfo); | 72 | static std::string GetErrorRegisters(const rtExceptionErrRegInfo& coreInfo); |
| 52 | - static void DumpErrorSymbols(const rtExceptionInfo& exception); | 73 | + // 默认(非回调)异常路径:同步落 _host.o,即刻 symbolize,并打印分类汇总。 |
| 53 | - static void DumpErrorSymbols(const rtExceptionInfo& exception, ExceptionRegInfo& exceptionRegInfo); | 74 | + static void DumpErrorSymbols(const rtExceptionInfo& exception, const std::string& dumpPath); |
| 75 | + static void DumpErrorSymbols(const rtExceptionInfo& exception, ExceptionRegInfo& exceptionRegInfo, | ||
| 76 | + const std::string& dumpPath); | ||
| 77 | + // 依据 _host.o 是否含 .debug_line 决定实际 symbolize 用的 .o,无调试信息时返回空串。 | ||
| 78 | + static std::string ResolveOFilePath(const std::string& hostOPath); | ||
| 79 | + | ||
| 80 | + // 按 (oFilePath, fixedPCOffset) 对异常 core 聚类,统一打印分类汇总到 adump 日志。 | ||
| 81 | + static void PrintClassificationSummary(const std::vector<ErrorLocation>& locations); | ||
| 54 | 82 | ||
| 55 | static void ClearCache(); | 83 | static void ClearCache(); |
| 56 | 84 | ||
| @@ -59,9 +87,17 @@ private: | |||
| 59 | uint64_t kernelDeviceStartPC_ = 0; | 87 | uint64_t kernelDeviceStartPC_ = 0; |
| 60 | bool hasKernelDeviceStartPC_ = false; | 88 | bool hasKernelDeviceStartPC_ = false; |
| 61 | bool initialized_ = false; | 89 | bool initialized_ = false; |
| 90 | + std::string oFilePath_; | ||
| 62 | 91 | ||
| 63 | int32_t ParseElfSymbols(const char* elf, size_t elfSize, KernelSymbolSet& symbols); | 92 | int32_t ParseElfSymbols(const char* elf, size_t elfSize, KernelSymbolSet& symbols); |
| 64 | - void PrintErrorForCore(rtExceptionErrRegInfo_t coreInfo); | 93 | + // 定位单个 core 的错误寄存器/PC/symbol 并回填 outLocation(不做 symbolize,源码解析统一批量执行)。 |
| 94 | + void PrintErrorForCore(rtExceptionErrRegInfo_t coreInfo, ErrorLocation& outLocation); | ||
| 95 | + // 按 fixedPCOffset 匹配最优符号并回填 outLocation,未命中时打印符号区间辅助定位。 | ||
| 96 | + void MatchSymbolForCore(const rtExceptionErrRegInfo_t& coreInfo, uint64_t fixedPCOffset, | ||
| 97 | + ErrorLocation& outLocation); | ||
| 98 | + // 对同一 .o(oFilePath_) 下所有未跳过 core 的偏移一次性批量 symbolize,按序回填各 loc.src。 | ||
| 99 | + // 单进程处理全部偏移,最坏耗时收敛为单次超时,避免逐核各起进程导致的 coreNum×timeout 放大。 | ||
| 100 | + void SymbolizeCollectedLocations(std::vector<ErrorLocation>& locations) const; | ||
| 65 | void ResetState(); | 101 | void ResetState(); |
| 66 | bool GetCorrectedStartPC(const rtExceptionErrRegInfo& coreInfo, uint64_t& startPC) const; | 102 | bool GetCorrectedStartPC(const rtExceptionErrRegInfo& coreInfo, uint64_t& startPC) const; |
| 67 | 103 | ||
| @@ -80,6 +80,7 @@ set(adumpBaseSrcList | |||
| 80 | ${SRC_CODE_ROOT_PATH}/adump/exception/dump_file.cpp | 80 | ${SRC_CODE_ROOT_PATH}/adump/exception/dump_file.cpp |
| 81 | ${SRC_CODE_ROOT_PATH}/adump/exception/kernel_info_collector.cpp | 81 | ${SRC_CODE_ROOT_PATH}/adump/exception/kernel_info_collector.cpp |
| 82 | ${SRC_CODE_ROOT_PATH}/adump/exception/kernel_symbol_locator.cpp | 82 | ${SRC_CODE_ROOT_PATH}/adump/exception/kernel_symbol_locator.cpp |
| 83 | + ${SRC_CODE_ROOT_PATH}/adump/exception/kernel_source_symbolizer.cpp | ||
| 83 | ${SRC_CODE_ROOT_PATH}/adump/exception/kernel_pc_fixer.cpp | 84 | ${SRC_CODE_ROOT_PATH}/adump/exception/kernel_pc_fixer.cpp |
| 84 | ${SRC_CODE_ROOT_PATH}/adump/exception/dump_operator.cpp | 85 | ${SRC_CODE_ROOT_PATH}/adump/exception/dump_operator.cpp |
| 85 | ${SRC_CODE_ROOT_PATH}/adump/exception/dump_args.cpp | 86 | ${SRC_CODE_ROOT_PATH}/adump/exception/dump_args.cpp |
| @@ -132,6 +133,7 @@ set(adumpBaseUtestSrcList | |||
| 132 | ${ADUMP_BASE_UT_ROOT_PATH}/testcase/exception/dump_core_utest.cpp | 133 | ${ADUMP_BASE_UT_ROOT_PATH}/testcase/exception/dump_core_utest.cpp |
| 133 | ${ADUMP_BASE_UT_ROOT_PATH}/testcase/exception/dump_file_utest.cpp | 134 | ${ADUMP_BASE_UT_ROOT_PATH}/testcase/exception/dump_file_utest.cpp |
| 134 | ${ADUMP_BASE_UT_ROOT_PATH}/testcase/exception/kernel_symbol_locator_utest.cpp | 135 | ${ADUMP_BASE_UT_ROOT_PATH}/testcase/exception/kernel_symbol_locator_utest.cpp |
| 136 | + ${ADUMP_BASE_UT_ROOT_PATH}/testcase/exception/kernel_source_symbolizer_utest.cpp | ||
| 135 | ${ADUMP_BASE_UT_ROOT_PATH}/testcase/exception/kernel_pc_fixer_utest.cpp | 137 | ${ADUMP_BASE_UT_ROOT_PATH}/testcase/exception/kernel_pc_fixer_utest.cpp |
| 136 | ${ADUMP_BASE_UT_ROOT_PATH}/testcase/exception/dump_operator_extra_utest.cpp | 138 | ${ADUMP_BASE_UT_ROOT_PATH}/testcase/exception/dump_operator_extra_utest.cpp |
| 137 | ${ADUMP_BASE_UT_ROOT_PATH}/testcase/exception/exception_dumper_extra_utest.cpp | 139 | ${ADUMP_BASE_UT_ROOT_PATH}/testcase/exception/exception_dumper_extra_utest.cpp |
| @@ -327,7 +327,8 @@ TEST_F(DumpArgsCallbackUtest, Test_DumpKernelErrorSymbols_Basic) | |||
| 327 | ExceptionDumpInfo info = {0}; | 327 | ExceptionDumpInfo info = {0}; |
| 328 | 328 | ||
| 329 | DumpArgsCallback callback(exception, info, ws.Root()); | 329 | DumpArgsCallback callback(exception, info, ws.Root()); |
| 330 | - EXPECT_EQ(callback.DumpKernelErrorSymbols(), ADUMP_SUCCESS); | 330 | + ErrorLocation location; |
| 331 | + EXPECT_EQ(callback.DumpKernelErrorSymbols(location), ADUMP_SUCCESS); | ||
| 331 | } | 332 | } |
| 332 | 333 | ||
| 333 | TEST_F(DumpArgsCallbackUtest, Test_DumpKernelErrorSymbols_InitFailed) | 334 | TEST_F(DumpArgsCallbackUtest, Test_DumpKernelErrorSymbols_InitFailed) |
| @@ -341,7 +342,8 @@ TEST_F(DumpArgsCallbackUtest, Test_DumpKernelErrorSymbols_InitFailed) | |||
| 341 | MOCKER(&KernelSymbolLocator::InitFromBinHandle).stubs().will(returnValue(ADUMP_FAILED)); | 342 | MOCKER(&KernelSymbolLocator::InitFromBinHandle).stubs().will(returnValue(ADUMP_FAILED)); |
| 342 | 343 | ||
| 343 | DumpArgsCallback callback(exception, info, ws.Root()); | 344 | DumpArgsCallback callback(exception, info, ws.Root()); |
| 344 | - EXPECT_EQ(callback.DumpKernelErrorSymbols(), ADUMP_FAILED); | 345 | + ErrorLocation location; |
| 346 | + EXPECT_EQ(callback.DumpKernelErrorSymbols(location), ADUMP_FAILED); | ||
| 345 | } | 347 | } |
| 346 | 348 | ||
| 347 | TEST_F(DumpArgsCallbackUtest, Test_DumpKernelErrorSymbols_GetRegInfoFailed) | 349 | TEST_F(DumpArgsCallbackUtest, Test_DumpKernelErrorSymbols_GetRegInfoFailed) |
| @@ -356,7 +358,8 @@ TEST_F(DumpArgsCallbackUtest, Test_DumpKernelErrorSymbols_GetRegInfoFailed) | |||
| 356 | MOCKER_CPP(&ExceptionInfoCommon::GetExceptionRegInfo).stubs().will(returnValue(ADUMP_FAILED)); | 358 | MOCKER_CPP(&ExceptionInfoCommon::GetExceptionRegInfo).stubs().will(returnValue(ADUMP_FAILED)); |
| 357 | 359 | ||
| 358 | DumpArgsCallback callback(exception, info, ws.Root()); | 360 | DumpArgsCallback callback(exception, info, ws.Root()); |
| 359 | - EXPECT_EQ(callback.DumpKernelErrorSymbols(), ADUMP_FAILED); | 361 | + ErrorLocation location; |
| 362 | + EXPECT_EQ(callback.DumpKernelErrorSymbols(location), ADUMP_FAILED); | ||
| 360 | } | 363 | } |
| 361 | 364 | ||
| 362 | TEST_F(DumpArgsCallbackUtest, Test_DumpKernelErrorSymbols_LocateFailed) | 365 | TEST_F(DumpArgsCallbackUtest, Test_DumpKernelErrorSymbols_LocateFailed) |
| @@ -369,10 +372,11 @@ TEST_F(DumpArgsCallbackUtest, Test_DumpKernelErrorSymbols_LocateFailed) | |||
| 369 | 372 | ||
| 370 | MOCKER(&KernelSymbolLocator::InitFromBinHandle).stubs().will(returnValue(ADUMP_SUCCESS)); | 373 | MOCKER(&KernelSymbolLocator::InitFromBinHandle).stubs().will(returnValue(ADUMP_SUCCESS)); |
| 371 | MOCKER_CPP(&ExceptionInfoCommon::GetExceptionRegInfo).stubs().will(returnValue(ADUMP_SUCCESS)); | 374 | MOCKER_CPP(&ExceptionInfoCommon::GetExceptionRegInfo).stubs().will(returnValue(ADUMP_SUCCESS)); |
| 372 | - MOCKER(&KernelSymbolLocator::LocateAndPrintErrorSymbolsForCore).stubs().will(returnValue(ADUMP_FAILED)); | 375 | + MOCKER(&KernelSymbolLocator::LocateErrorSymbolsForCore).stubs().will(returnValue(ADUMP_FAILED)); |
| 373 | 376 | ||
| 374 | DumpArgsCallback callback(exception, info, ws.Root()); | 377 | DumpArgsCallback callback(exception, info, ws.Root()); |
| 375 | - EXPECT_EQ(callback.DumpKernelErrorSymbols(), ADUMP_FAILED); | 378 | + ErrorLocation location; |
| 379 | + EXPECT_EQ(callback.DumpKernelErrorSymbols(location), ADUMP_FAILED); | ||
| 376 | } | 380 | } |
| 377 | 381 | ||
| 378 | TEST_F(DumpArgsCallbackUtest, Test_DumpKernelErrorSymbols_LocateSuccess) | 382 | TEST_F(DumpArgsCallbackUtest, Test_DumpKernelErrorSymbols_LocateSuccess) |
| @@ -385,10 +389,11 @@ TEST_F(DumpArgsCallbackUtest, Test_DumpKernelErrorSymbols_LocateSuccess) | |||
| 385 | 389 | ||
| 386 | MOCKER(&KernelSymbolLocator::InitFromBinHandle).stubs().will(returnValue(ADUMP_SUCCESS)); | 390 | MOCKER(&KernelSymbolLocator::InitFromBinHandle).stubs().will(returnValue(ADUMP_SUCCESS)); |
| 387 | MOCKER_CPP(&ExceptionInfoCommon::GetExceptionRegInfo).stubs().will(returnValue(ADUMP_SUCCESS)); | 391 | MOCKER_CPP(&ExceptionInfoCommon::GetExceptionRegInfo).stubs().will(returnValue(ADUMP_SUCCESS)); |
| 388 | - MOCKER(&KernelSymbolLocator::LocateAndPrintErrorSymbolsForCore).stubs().will(returnValue(ADUMP_SUCCESS)); | 392 | + MOCKER(&KernelSymbolLocator::LocateErrorSymbolsForCore).stubs().will(returnValue(ADUMP_SUCCESS)); |
| 389 | 393 | ||
| 390 | DumpArgsCallback callback(exception, info, ws.Root()); | 394 | DumpArgsCallback callback(exception, info, ws.Root()); |
| 391 | - EXPECT_EQ(callback.DumpKernelErrorSymbols(), ADUMP_SUCCESS); | 395 | + ErrorLocation location; |
| 396 | + EXPECT_EQ(callback.DumpKernelErrorSymbols(location), ADUMP_SUCCESS); | ||
| 392 | } | 397 | } |
| 393 | 398 | ||
| 394 | TEST_F(DumpArgsCallbackUtest, Test_DumpDfxArgs_QueryDfxInfoGetFunctionFailed) | 399 | TEST_F(DumpArgsCallbackUtest, Test_DumpDfxArgs_QueryDfxInfoGetFunctionFailed) |
| @@ -2563,14 +2563,17 @@ TEST_F(DumpArgsUtest, Test_DumpHostKernelBin_Idempotent) | |||
| 2563 | ASSERT_EQ(collector.InitFromBinHandle(static_cast<rtBinHandle>(fakeHandle), kernelName), ADUMP_SUCCESS); | 2563 | ASSERT_EQ(collector.InitFromBinHandle(static_cast<rtBinHandle>(fakeHandle), kernelName), ADUMP_SUCCESS); |
| 2564 | 2564 | ||
| 2565 | // 首次落盘:文件不存在,正常写入。 | 2565 | // 首次落盘:文件不存在,正常写入。 |
| 2566 | - EXPECT_EQ(collector.DumpHostKernelBin(ws.Root()), ADUMP_SUCCESS); | 2566 | + std::string hostOPath; |
级别:提示 问题:DumpHostKernelBin 测试未验证新增的 outHostOPath 输出参数是否正确回传落盘路径 影响:outHostOPath 为空或错误时不会被测试捕获,后续 symbolize 拿到错误路径 修复建议:在成功用例中增加 EXPECT_EQ(hostOPath, ws.Root()+"/"+kernelName+"_host.o") 等断言 ![]() ![]() sinat_31531339 Aug 11 Comments: Aug 11 Comments: ![]() ![]() | |||
| 2567 | + EXPECT_EQ(collector.DumpHostKernelBin(ws.Root(), hostOPath), ADUMP_SUCCESS); | ||
| 2567 | Path hostBinPath(ws.Root()); | 2568 | Path hostBinPath(ws.Root()); |
| 2568 | hostBinPath.Concat(kernelName + "_host.o"); | 2569 | hostBinPath.Concat(kernelName + "_host.o"); |
| 2569 | EXPECT_TRUE(hostBinPath.Exist()); | 2570 | EXPECT_TRUE(hostBinPath.Exist()); |
| 2571 | + // 出参应回填实际落盘路径,且与文件系统上的 _host.o 一致。 | ||
| 2572 | + EXPECT_EQ(hostOPath, hostBinPath.GetString()); | ||
| 2570 | 2573 | ||
| 2571 | // 二次落盘:文件已存在且大小一致。即使 File::Write 被打桩为失败,幂等守卫也应跳过写并返回成功。 | 2574 | // 二次落盘:文件已存在且大小一致。即使 File::Write 被打桩为失败,幂等守卫也应跳过写并返回成功。 |
| 2572 | MOCKER_CPP(&File::Write).expects(never()); | 2575 | MOCKER_CPP(&File::Write).expects(never()); |
| 2573 | - EXPECT_EQ(collector.DumpHostKernelBin(ws.Root()), ADUMP_SUCCESS); | 2576 | + EXPECT_EQ(collector.DumpHostKernelBin(ws.Root(), hostOPath), ADUMP_SUCCESS); |
| 2574 | GlobalMockObject::verify(); | 2577 | GlobalMockObject::verify(); |
| 2575 | } | 2578 | } |
| 2576 | 2579 | ||
| @@ -2586,7 +2589,8 @@ TEST_F(DumpArgsUtest, Test_DumpHostKernelBin_EmptyKernelNameSkip) | |||
| 2586 | ASSERT_EQ(collector.InitFromBinHandle(static_cast<rtBinHandle>(fakeHandle), ""), ADUMP_SUCCESS); | 2589 | ASSERT_EQ(collector.InitFromBinHandle(static_cast<rtBinHandle>(fakeHandle), ""), ADUMP_SUCCESS); |
| 2587 | 2590 | ||
| 2588 | // 前置守卫命中,跳过写、返回成功,且不产生 "_host.o"。 | 2591 | // 前置守卫命中,跳过写、返回成功,且不产生 "_host.o"。 |
| 2589 | - EXPECT_EQ(collector.DumpHostKernelBin(ws.Root()), ADUMP_SUCCESS); | 2592 | + std::string hostOPath; |
| 2593 | + EXPECT_EQ(collector.DumpHostKernelBin(ws.Root(), hostOPath), ADUMP_SUCCESS); | ||
| 2590 | Path hostBinPath(ws.Root()); | 2594 | Path hostBinPath(ws.Root()); |
| 2591 | hostBinPath.Concat("_host.o"); | 2595 | hostBinPath.Concat("_host.o"); |
| 2592 | EXPECT_FALSE(hostBinPath.Exist()); | 2596 | EXPECT_FALSE(hostBinPath.Exist()); |
| @@ -2613,7 +2617,9 @@ TEST_F(DumpArgsUtest, Test_DumpHostKernelBin_TruncatedFileRewrite) | |||
| 2613 | ASSERT_TRUE(hostBinPath.Exist()); | 2617 | ASSERT_TRUE(hostBinPath.Exist()); |
| 2614 | 2618 | ||
| 2615 | // 大小不匹配,幂等守卫不跳过,应重写为完整内容。 | 2619 | // 大小不匹配,幂等守卫不跳过,应重写为完整内容。 |
| 2616 | - EXPECT_EQ(collector.DumpHostKernelBin(ws.Root()), ADUMP_SUCCESS); | 2620 | + std::string hostOPath; |
| 2621 | + EXPECT_EQ(collector.DumpHostKernelBin(ws.Root(), hostOPath), ADUMP_SUCCESS); | ||
| 2622 | + EXPECT_EQ(hostOPath, hostBinPath.GetString()); | ||
| 2617 | std::ifstream rewritten(hostBinPath.GetString(), std::ios::binary); | 2623 | std::ifstream rewritten(hostBinPath.GetString(), std::ios::binary); |
| 2618 | std::string content((std::istreambuf_iterator<char>(rewritten)), std::istreambuf_iterator<char>()); | 2624 | std::string content((std::istreambuf_iterator<char>(rewritten)), std::istreambuf_iterator<char>()); |
| 2619 | EXPECT_EQ(content, g_idemHostBinContent); | 2625 | EXPECT_EQ(content, g_idemHostBinContent); |
| @@ -2632,6 +2638,7 @@ TEST_F(DumpArgsUtest, Test_DumpHostKernelBin_ShortWriteFail) | |||
| 2632 | char fakeHandle[] = "fake_bin_handle"; | 2638 | char fakeHandle[] = "fake_bin_handle"; |
| 2633 | ASSERT_EQ(collector.InitFromBinHandle(static_cast<rtBinHandle>(fakeHandle), kernelName), ADUMP_SUCCESS); | 2639 | ASSERT_EQ(collector.InitFromBinHandle(static_cast<rtBinHandle>(fakeHandle), kernelName), ADUMP_SUCCESS); |
| 2634 | 2640 | ||
| 2635 | - EXPECT_EQ(collector.DumpHostKernelBin(ws.Root()), ADUMP_FAILED); | 2641 | + std::string hostOPath; |
| 2642 | + EXPECT_EQ(collector.DumpHostKernelBin(ws.Root(), hostOPath), ADUMP_FAILED); | ||
| 2636 | GlobalMockObject::verify(); | 2643 | GlobalMockObject::verify(); |
| 2637 | } | 2644 | } |
| @@ -15,8 +15,13 @@ | |||
| 15 | #include "common/thread.h" | 15 | #include "common/thread.h" |
| 16 | #include "adx_dump_record.h" | 16 | #include "adx_dump_record.h" |
| 17 | #include "dump_exception_stub.h" | 17 | #include "dump_exception_stub.h" |
| 18 | +#include "kernel_symbol_locator.h" | ||
| 19 | +#include "kernel_info_collector.h" | ||
| 20 | +#include "exception_info_common.h" | ||
| 21 | +#include "path.h" | ||
| 18 | #include <gtest/gtest.h> | 22 | #include <gtest/gtest.h> |
| 19 | #include "mockcpp/mockcpp.hpp" | 23 | #include "mockcpp/mockcpp.hpp" |
| 24 | +#include <fstream> | ||
| 20 | 25 | ||
| 21 | using namespace Adx; | 26 | using namespace Adx; |
| 22 | 27 | ||
| @@ -802,3 +807,68 @@ TEST_F(ExceptionDumperExtraUtest, DumpArgsExceptionInner_EmptyNamesAllowed) | |||
| 802 | ret = dumper.DumpException(exception); | 807 | ret = dumper.DumpException(exception); |
| 803 | EXPECT_EQ(ret, ADUMP_SUCCESS); | 808 | EXPECT_EQ(ret, ADUMP_SUCCESS); |
| 804 | } | 809 | } |
| 810 | + | ||
| 811 | +namespace { | ||
| 812 | +const std::string g_hostBinContentForSymFail = "host kernel bin content for symbolize-fail regression"; | ||
| 813 | +int32_t StubGetBinDataForSymFail(rtBinHandle binHandle, std::string &binData, uint32_t &binSize) | ||
| 814 | +{ | ||
| 815 | + (void)binHandle; | ||
| 816 | + binData = g_hostBinContentForSymFail; | ||
| 817 | + binSize = static_cast<uint32_t>(g_hostBinContentForSymFail.size()); | ||
| 818 | + return ADUMP_SUCCESS; | ||
| 819 | +} | ||
| 820 | +} // namespace | ||
| 821 | + | ||
| 822 | +// 编排层端到端回归:DumpException(args 默认路径) 先无条件 DumpHostKernelBinBeforeSymbolize 落 _host.o, | ||
| 823 | +// 再调 KernelSymbolLocator::DumpErrorSymbols 做符号化。这里打桩符号解析(InitFromBinBuffer)失败, | ||
| 824 | +// 模拟 .o 符号表损坏/无法解析导致 DumpErrorSymbols 在 InitFromBinBuffer 处提前 return。 | ||
| 825 | +// 此时 _host.o 仍必须已落盘且内容完整,守护「提前落盘独立于符号化」的编排不变量: | ||
| 826 | +// 防止落盘被误合并进 DumpErrorSymbols 内部、被前置符号解析的提前 return 挡住而漏落盘。 | ||
| 827 | +// 若回退该修复(删除 DumpHostKernelBinBeforeSymbolize,落盘只留在 DumpErrorSymbols 内),本用例会因 _host.o 缺失而失败。 | ||
| 828 | +TEST_F(ExceptionDumperExtraUtest, DumpArgsException_HostBinDroppedWhenSymbolizeFails) | ||
| 829 | +{ | ||
| 830 | + const uint32_t deviceId = 0U; | ||
| 831 | + const std::string dumpRoot = "/tmp/adump_hostbin_symfail_test"; | ||
| 832 | + const std::string kernelName = "AddCustom_6ee04b5d550e4239498c29151be6bb50_mix_aic"; | ||
| 833 | + | ||
| 834 | + ExceptionDumper dumper; | ||
| 835 | + DumpConfig config; | ||
| 836 | + config.dumpStatus = "on"; | ||
| 837 | + config.dumpPath = dumpRoot; | ||
| 838 | + ASSERT_EQ(dumper.ExceptionDumperInit(DumpType::ARGS_EXCEPTION, config), ADUMP_SUCCESS); | ||
| 839 | + | ||
| 840 | + // 落盘链依赖 GetBinDataFromHandle 返回 bin buffer,打桩成功以确保 _host.o 能落盘且内容可校验。 | ||
| 841 | + MOCKER_CPP(&ExceptionInfoCommon::GetBinDataFromHandle).stubs().will(invoke(StubGetBinDataForSymFail)); | ||
| 842 | + // 关键:符号解析失败,使 DumpErrorSymbols 在 InitFromBinBuffer 处提前 return,不再落盘。 | ||
| 843 | + MOCKER_CPP(&KernelSymbolLocator::InitFromBinBuffer) | ||
| 844 | + .stubs() | ||
| 845 | + .will(returnValue(static_cast<int32_t>(ADUMP_FAILED))); | ||
| 846 | + | ||
| 847 | + char hostKernel[] = "host kernel bin file stub"; | ||
| 848 | + rtExceptionInfo exception = {}; | ||
| 849 | + exception.deviceid = deviceId; | ||
| 850 | + exception.taskid = 1U; | ||
| 851 | + exception.streamid = 2U; | ||
| 852 | + exception.expandInfo.type = RT_EXCEPTION_AICORE; | ||
| 853 | + auto &kernelInfo = exception.expandInfo.u.aicoreInfo.exceptionArgs.exceptionKernelInfo; | ||
| 854 | + kernelInfo.bin = static_cast<rtBinHandle>(hostKernel); | ||
| 855 | + kernelInfo.binSize = sizeof(hostKernel); | ||
| 856 | + kernelInfo.kernelName = const_cast<char *>(kernelName.data()); | ||
| 857 | + kernelInfo.kernelNameSize = kernelName.size(); | ||
| 858 | + | ||
| 859 | + // 最终返回值不作断言:后续 args 解析在无真实 device args 时可能失败, | ||
| 860 | + // 但那发生在 _host.o 落盘之后,不影响本用例要守护的落盘不变量。 | ||
| 861 | + (void)dumper.DumpException(exception); | ||
| 862 | + | ||
| 863 | + // 符号解析失败不影响提前落盘:_host.o 仍应存在且内容完整。 | ||
| 864 | + // _host.o 名去掉 _mix_aic 后缀,落在 device dump 目录(<root>/extra-info/data-dump/<deviceId>)下。 | ||
| 865 | + Path hostBinPath(dumpRoot); | ||
| 866 | + hostBinPath.Append("/extra-info/data-dump/").Append(std::to_string(deviceId)); | ||
| 867 | + std::string hostOName = ExceptionInfoCommon::GetKernelNameWithoutMixSuffix(kernelName) + "_host.o"; | ||
| 868 | + hostBinPath.Concat(hostOName); | ||
| 869 | + EXPECT_TRUE(hostBinPath.Exist()); | ||
| 870 | + std::ifstream dumped(hostBinPath.GetString(), std::ios::binary); | ||
| 871 | + std::string content((std::istreambuf_iterator<char>(dumped)), std::istreambuf_iterator<char>()); | ||
| 872 | + EXPECT_EQ(content, g_hostBinContentForSymFail); | ||
| 873 | + GlobalMockObject::verify(); | ||
| 874 | +} | ||
| @@ -0,0 +1,370 @@ | |||
| 1 | +/** | ||
级别:提示 问题:缺少 RunSymbolizer 超时路径(子进程 hang)的测试用例 影响:超时 kill + waitpid 的资源清理逻辑未被覆盖,回归风险 修复建议:增加一个假 symbolizer 脚本(sleep 10),验证超时后返回 false 且无僵尸进程 ![]() ![]() | |||
| 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 | +#include <gtest/gtest.h> | ||
| 11 | +#include <chrono> | ||
| 12 | +#include <cstdint> | ||
| 13 | +#include <cstdio> | ||
| 14 | +#include <cstdlib> | ||
| 15 | +#include <elf.h> | ||
| 16 | +#include <fstream> | ||
| 17 | +#include <string> | ||
| 18 | +#include <vector> | ||
| 19 | +#include <unistd.h> | ||
| 20 | +#include "securec.h" | ||
| 21 | +#include "kernel_source_symbolizer.h" | ||
| 22 | +#include "kernel_symbol_locator.h" | ||
| 23 | +#include "tools/case_workspace.h" | ||
| 24 | + | ||
| 25 | +using namespace Adx; | ||
| 26 | + | ||
| 27 | +namespace { | ||
| 28 | +// 填充最小合法的 ELF64 文件头:magic、64 位小端、section header 表位置与数量。 | ||
| 29 | +void FillElfHeader(Elf64_Ehdr &eh, size_t shOff, uint16_t shnum) | ||
| 30 | +{ | ||
| 31 | + eh.e_ident[EI_MAG0] = ELFMAG0; | ||
| 32 | + eh.e_ident[EI_MAG1] = ELFMAG1; | ||
| 33 | + eh.e_ident[EI_MAG2] = ELFMAG2; | ||
| 34 | + eh.e_ident[EI_MAG3] = ELFMAG3; | ||
| 35 | + eh.e_ident[EI_CLASS] = ELFCLASS64; | ||
| 36 | + eh.e_ident[EI_DATA] = ELFDATA2LSB; | ||
| 37 | + eh.e_ident[EI_VERSION] = EV_CURRENT; | ||
| 38 | + eh.e_type = ET_REL; | ||
| 39 | + eh.e_machine = EM_AARCH64; | ||
| 40 | + eh.e_version = EV_CURRENT; | ||
| 41 | + eh.e_ehsize = sizeof(Elf64_Ehdr); | ||
| 42 | + eh.e_shentsize = sizeof(Elf64_Shdr); | ||
| 43 | + eh.e_shoff = shOff; | ||
| 44 | + eh.e_shnum = shnum; | ||
| 45 | + eh.e_shstrndx = 1; // .shstrtab 为第 1 个段 | ||
| 46 | +} | ||
| 47 | + | ||
| 48 | +// 构造一个最小 ELF64,可选包含名为 sectionName 的段(用于 .debug_line 探测)。 | ||
| 49 | +std::string MakeElfWithSection(bool withSection, const std::string §ionName = ".debug_line") | ||
| 50 | +{ | ||
| 51 | + // 段名字符串表:首字节 '\0',随后依次是各段名。 | ||
| 52 | + std::string shstr; | ||
| 53 | + shstr.push_back('\0'); | ||
| 54 | + uint32_t shstrtabNameOff = static_cast<uint32_t>(shstr.size()); | ||
| 55 | + shstr += ".shstrtab"; | ||
| 56 | + shstr.push_back('\0'); | ||
| 57 | + uint32_t targetNameOff = 0; | ||
| 58 | + if (withSection) { | ||
| 59 | + targetNameOff = static_cast<uint32_t>(shstr.size()); | ||
| 60 | + shstr += sectionName; | ||
| 61 | + shstr.push_back('\0'); | ||
| 62 | + } | ||
| 63 | + | ||
| 64 | + const uint16_t shnum = withSection ? 3 : 2; // NULL + .shstrtab (+ target) | ||
| 65 | + const size_t shOff = sizeof(Elf64_Ehdr); | ||
| 66 | + const size_t shstrOff = shOff + static_cast<size_t>(shnum) * sizeof(Elf64_Shdr); | ||
| 67 | + std::string buf; | ||
| 68 | + buf.resize(shstrOff + shstr.size(), '\0'); | ||
| 69 | + | ||
| 70 | + Elf64_Ehdr eh{}; | ||
| 71 | + FillElfHeader(eh, shOff, shnum); | ||
| 72 | + (void)memcpy_s(&buf[0], sizeof(eh), &eh, sizeof(eh)); | ||
| 73 | + | ||
| 74 | + std::vector<Elf64_Shdr> shdrs(shnum); | ||
| 75 | + shdrs[0].sh_type = SHT_NULL; // [0] NULL,sh_name 默认 0 | ||
| 76 | + shdrs[1].sh_name = shstrtabNameOff; // [1] .shstrtab | ||
| 77 | + shdrs[1].sh_type = SHT_STRTAB; | ||
| 78 | + shdrs[1].sh_offset = shstrOff; | ||
| 79 | + shdrs[1].sh_size = shstr.size(); | ||
| 80 | + if (withSection) { | ||
| 81 | + shdrs[2].sh_name = targetNameOff; // [2] target section | ||
| 82 | + shdrs[2].sh_type = SHT_PROGBITS; | ||
| 83 | + shdrs[2].sh_offset = shstrOff; // 复用字符串表数据区,探测只看段名不看内容 | ||
| 84 | + shdrs[2].sh_size = 1; | ||
| 85 | + } | ||
| 86 | + for (uint16_t i = 0; i < shnum; ++i) { | ||
| 87 | + (void)memcpy_s(&buf[shOff + i * sizeof(Elf64_Shdr)], sizeof(Elf64_Shdr), &shdrs[i], sizeof(Elf64_Shdr)); | ||
| 88 | + } | ||
| 89 | + (void)memcpy_s(&buf[shstrOff], shstr.size(), shstr.data(), shstr.size()); | ||
| 90 | + return buf; | ||
| 91 | +} | ||
| 92 | + | ||
| 93 | +void WriteFile(const std::string &path, const std::string &content) | ||
| 94 | +{ | ||
| 95 | + std::ofstream ofs(path, std::ios::binary | std::ios::trunc); | ||
| 96 | + ofs.write(content.data(), static_cast<std::streamsize>(content.size())); | ||
| 97 | + ofs.close(); | ||
| 98 | +} | ||
| 99 | + | ||
| 100 | +// 写一个假的 llvm-symbolizer 脚本:从 stdin 读偏移,按行输出 func 与 file:line:col。 | ||
| 101 | +std::string WriteFakeSymbolizer(const std::string &path, const std::string &body) | ||
| 102 | +{ | ||
| 103 | + WriteFile(path, body); | ||
| 104 | + (void)chmod(path.c_str(), 0755); | ||
| 105 | + return path; | ||
| 106 | +} | ||
| 107 | + | ||
| 108 | +class KernelSourceSymbolizerUTest : public testing::Test { | ||
| 109 | +protected: | ||
| 110 | + void SetUp() override | ||
| 111 | + { | ||
| 112 | + (void)unsetenv("ADUMP_LLVM_SYMBOLIZER"); | ||
| 113 | + KernelSourceSymbolizer::ResetLocateCacheForTest(); | ||
| 114 | + } | ||
| 115 | + void TearDown() override | ||
| 116 | + { | ||
| 117 | + (void)unsetenv("ADUMP_LLVM_SYMBOLIZER"); | ||
| 118 | + KernelSourceSymbolizer::ResetLocateCacheForTest(); | ||
| 119 | + } | ||
| 120 | +}; | ||
| 121 | + | ||
| 122 | +TEST_F(KernelSourceSymbolizerUTest, HasDebugLineDetectsSection) | ||
| 123 | +{ | ||
| 124 | + Tools::CaseWorkspace ws("HasDebugLineDetectsSection"); | ||
| 125 | + const std::string withPath = ws.Root() + "/with_debug.o"; | ||
| 126 | + const std::string withoutPath = ws.Root() + "/without_debug.o"; | ||
| 127 | + WriteFile(withPath, MakeElfWithSection(true)); | ||
| 128 | + WriteFile(withoutPath, MakeElfWithSection(false)); | ||
| 129 | + | ||
| 130 | + EXPECT_TRUE(KernelSourceSymbolizer::HasDebugLine(withPath)); | ||
| 131 | + EXPECT_FALSE(KernelSourceSymbolizer::HasDebugLine(withoutPath)); | ||
| 132 | + EXPECT_FALSE(KernelSourceSymbolizer::HasDebugLine(ws.Root() + "/not_exist.o")); | ||
| 133 | +} | ||
| 134 | + | ||
| 135 | +TEST_F(KernelSourceSymbolizerUTest, NotAvailableWhenToolMissing) | ||
| 136 | +{ | ||
| 137 | + (void)setenv("ADUMP_LLVM_SYMBOLIZER", "/nonexistent/path/llvm-symbolizer", 1); | ||
| 138 | + // IsAvailable 结果带缓存,进程内首次即固化,此处仅验证不崩溃且 Symbolize 优雅降级。 | ||
| 139 | + std::vector<uint64_t> offsets{0x10}; | ||
| 140 | + std::vector<SymbolizeResult> results; | ||
| 141 | + // 无论缓存状态如何,Symbolize 在工具不可用或 .o 无效时都应返回 false 且不崩溃。 | ||
| 142 | + EXPECT_FALSE(KernelSourceSymbolizer::Symbolize("/nonexistent/x.o", offsets, results)); | ||
| 143 | +} | ||
| 144 | + | ||
| 145 | +TEST_F(KernelSourceSymbolizerUTest, SymbolizeParsesOutput) | ||
| 146 | +{ | ||
| 147 | + Tools::CaseWorkspace ws("SymbolizeParsesOutput"); | ||
| 148 | + const std::string oPath = ws.Root() + "/mod.o"; | ||
| 149 | + WriteFile(oPath, MakeElfWithSection(true)); | ||
| 150 | + | ||
| 151 | + // 假 symbolizer:为每个输入偏移输出「函数名行 + 位置行 + 空行分隔」。第一个命中、第二个未知。 | ||
| 152 | + // 函数名行故意含冒号(ns::add(int)),验证解析端凭"结尾:数字:数字"识别位置行、不误取函数名行。 | ||
| 153 | + const std::string script = | ||
| 154 | + "#!/bin/sh\n" | ||
| 155 | + "i=0\n" | ||
| 156 | + "while read line; do\n" | ||
| 157 | + " if [ \"$i\" = \"0\" ]; then echo 'ns::add(int)'; echo /path/add.cpp:88:12;\n" | ||
| 158 | + " else echo '??'; echo '??:0:0'; fi\n" | ||
| 159 | + " echo ''\n" | ||
| 160 | + " i=$((i+1))\n" | ||
| 161 | + "done\n"; | ||
| 162 | + const std::string tool = WriteFakeSymbolizer(ws.Root() + "/fake-symbolizer", script); | ||
| 163 | + // 先设置环境变量,再重置缓存,确保 LocateSymbolizer 首次解析即命中本用例的假工具。 | ||
| 164 | + (void)setenv("ADUMP_LLVM_SYMBOLIZER", tool.c_str(), 1); | ||
| 165 | + KernelSourceSymbolizer::ResetLocateCacheForTest(); | ||
| 166 | + | ||
| 167 | + std::vector<uint64_t> offsets{0x1a4, 0x3c}; | ||
| 168 | + std::vector<SymbolizeResult> results; | ||
| 169 | + ASSERT_TRUE(KernelSourceSymbolizer::Symbolize(oPath, offsets, results)); | ||
| 170 | + ASSERT_EQ(results.size(), 2U); | ||
| 171 | + EXPECT_TRUE(results[0].ok); | ||
| 172 | + EXPECT_EQ(results[0].srcFile, "/path/add.cpp"); | ||
| 173 | + EXPECT_EQ(results[0].srcLine, 88U); | ||
| 174 | + EXPECT_EQ(results[0].srcColumn, 12U); | ||
| 175 | + EXPECT_FALSE(results[1].ok); | ||
| 176 | +} | ||
| 177 | + | ||
| 178 | +TEST_F(KernelSourceSymbolizerUTest, SymbolizeEmptyOffsetsReturnsFalse) | ||
| 179 | +{ | ||
| 180 | + std::vector<uint64_t> offsets; | ||
| 181 | + std::vector<SymbolizeResult> results; | ||
| 182 | + EXPECT_FALSE(KernelSourceSymbolizer::Symbolize("/whatever.o", offsets, results)); | ||
| 183 | + EXPECT_TRUE(results.empty()); | ||
| 184 | +} | ||
| 185 | + | ||
| 186 | +// 同一个 .o 内多个偏移一次解析:一个进程按序处理,结果与 offsets 一一对应回填。 | ||
| 187 | +TEST_F(KernelSourceSymbolizerUTest, SymbolizeMultipleOffsetsInOneOFile) | ||
| 188 | +{ | ||
| 189 | + Tools::CaseWorkspace ws("SymbolizeMultipleOffsetsInOneOFile"); | ||
| 190 | + const std::string addO = ws.Root() + "/add.o"; | ||
| 191 | + WriteFile(addO, MakeElfWithSection(true)); | ||
| 192 | + | ||
| 193 | + // 假 symbolizer:按输入行序,每块输出 func_<i> 行、位置行 file_<i>.cpp:<10+i>:1,再补空行分隔。 | ||
| 194 | + const std::string script = | ||
| 195 | + "#!/bin/sh\n" | ||
| 196 | + "i=0\n" | ||
| 197 | + "while read line; do\n" | ||
| 198 | + " echo func_$i\n" | ||
| 199 | + " echo /src/file_$i.cpp:$((10+i)):1\n" | ||
| 200 | + " echo ''\n" | ||
| 201 | + " i=$((i+1))\n" | ||
| 202 | + "done\n"; | ||
| 203 | + const std::string tool = WriteFakeSymbolizer(ws.Root() + "/fake-symbolizer", script); | ||
| 204 | + (void)setenv("ADUMP_LLVM_SYMBOLIZER", tool.c_str(), 1); | ||
| 205 | + KernelSourceSymbolizer::ResetLocateCacheForTest(); | ||
| 206 | + | ||
| 207 | + std::vector<uint64_t> offsets{0x1a4, 0x3c, 0x20}; | ||
| 208 | + std::vector<SymbolizeResult> results; | ||
| 209 | + ASSERT_TRUE(KernelSourceSymbolizer::Symbolize(addO, offsets, results)); | ||
| 210 | + ASSERT_EQ(results.size(), 3U); | ||
| 211 | + EXPECT_TRUE(results[0].ok); | ||
| 212 | + EXPECT_EQ(results[0].srcFile, "/src/file_0.cpp"); | ||
| 213 | + EXPECT_EQ(results[0].srcLine, 10U); | ||
| 214 | + EXPECT_TRUE(results[1].ok); | ||
| 215 | + EXPECT_EQ(results[1].srcFile, "/src/file_1.cpp"); | ||
| 216 | + EXPECT_EQ(results[1].srcLine, 11U); | ||
| 217 | + EXPECT_TRUE(results[2].ok); | ||
| 218 | + EXPECT_EQ(results[2].srcFile, "/src/file_2.cpp"); | ||
| 219 | + EXPECT_EQ(results[2].srcLine, 12U); | ||
| 220 | +} | ||
| 221 | + | ||
| 222 | +// 内联展开:一个偏移输出多帧(最内层内联 -> 外层),块内取第一条位置行即最内层帧源码位置。 | ||
| 223 | +TEST_F(KernelSourceSymbolizerUTest, SymbolizeParsesInlinedFramesTakesInnermostSource) | ||
| 224 | +{ | ||
| 225 | + Tools::CaseWorkspace ws("SymbolizeParsesInlinedFramesTakesInnermostSource"); | ||
| 226 | + const std::string oPath = ws.Root() + "/inl.o"; | ||
| 227 | + WriteFile(oPath, MakeElfWithSection(true)); | ||
| 228 | + | ||
| 229 | + // 单偏移多帧:内层 f_inner@/inc/a.h:5:3、外层 f_outer@/src/a.cpp:20:7,块尾空行分隔。 | ||
| 230 | + const std::string script = | ||
| 231 | + "#!/bin/sh\n" | ||
| 232 | + "read line\n" | ||
| 233 | + "echo f_inner\n" | ||
| 234 | + "echo /inc/a.h:5:3\n" | ||
| 235 | + "echo f_outer\n" | ||
| 236 | + "echo /src/a.cpp:20:7\n" | ||
| 237 | + "echo ''\n"; | ||
| 238 | + const std::string tool = WriteFakeSymbolizer(ws.Root() + "/fake-symbolizer", script); | ||
| 239 | + (void)setenv("ADUMP_LLVM_SYMBOLIZER", tool.c_str(), 1); | ||
| 240 | + KernelSourceSymbolizer::ResetLocateCacheForTest(); | ||
| 241 | + | ||
| 242 | + std::vector<uint64_t> offsets{0x1a4}; | ||
| 243 | + std::vector<SymbolizeResult> results; | ||
| 244 | + ASSERT_TRUE(KernelSourceSymbolizer::Symbolize(oPath, offsets, results)); | ||
| 245 | + ASSERT_EQ(results.size(), 1U); | ||
| 246 | + EXPECT_TRUE(results[0].ok); | ||
| 247 | + EXPECT_EQ(results[0].srcFile, "/inc/a.h"); // 取最内层帧 | ||
| 248 | + EXPECT_EQ(results[0].srcLine, 5U); | ||
| 249 | + EXPECT_EQ(results[0].srcColumn, 3U); | ||
| 250 | +} | ||
| 251 | + | ||
| 252 | +// 不存在的 .o:整体解析失败返回 false,results 保持占位(ok=false)。 | ||
| 253 | +TEST_F(KernelSourceSymbolizerUTest, SymbolizeInvalidOFileReturnsFalse) | ||
| 254 | +{ | ||
| 255 | + Tools::CaseWorkspace ws("SymbolizeInvalidOFileReturnsFalse"); | ||
| 256 | + const std::string script = "#!/bin/sh\ncat >/dev/null\n"; | ||
| 257 | + const std::string tool = WriteFakeSymbolizer(ws.Root() + "/fake-symbolizer", script); | ||
| 258 | + (void)setenv("ADUMP_LLVM_SYMBOLIZER", tool.c_str(), 1); | ||
| 259 | + KernelSourceSymbolizer::ResetLocateCacheForTest(); | ||
| 260 | + | ||
| 261 | + std::vector<uint64_t> offsets{0x10, 0x20}; | ||
| 262 | + std::vector<SymbolizeResult> results; | ||
| 263 | + EXPECT_FALSE(KernelSourceSymbolizer::Symbolize(ws.Root() + "/not_exist.o", offsets, results)); | ||
| 264 | + ASSERT_EQ(results.size(), 2U); | ||
| 265 | + EXPECT_FALSE(results[0].ok); | ||
| 266 | + EXPECT_FALSE(results[1].ok); | ||
| 267 | +} | ||
| 268 | + | ||
| 269 | +// 超时路径:假 symbolizer 收到输入后长时间不输出(sleep 远超 3s 超时), | ||
| 270 | +// 父进程应在 deadline 到达后 kill 子进程、waitpid 回收(无僵尸),并返回 false。 | ||
| 271 | +TEST_F(KernelSourceSymbolizerUTest, SymbolizeTimesOutAndReapsChild) | ||
| 272 | +{ | ||
| 273 | + Tools::CaseWorkspace ws("SymbolizeTimesOutAndReapsChild"); | ||
| 274 | + const std::string oPath = ws.Root() + "/hang.o"; | ||
| 275 | + WriteFile(oPath, MakeElfWithSection(true)); | ||
| 276 | + | ||
| 277 | + // 读走一行输入后睡眠很久且不产出任何输出,逼父进程走超时分支。 | ||
| 278 | + const std::string script = | ||
| 279 | + "#!/bin/sh\n" | ||
| 280 | + "read line\n" | ||
| 281 | + "sleep 30\n"; | ||
| 282 | + const std::string tool = WriteFakeSymbolizer(ws.Root() + "/fake-symbolizer", script); | ||
| 283 | + (void)setenv("ADUMP_LLVM_SYMBOLIZER", tool.c_str(), 1); | ||
| 284 | + KernelSourceSymbolizer::ResetLocateCacheForTest(); | ||
| 285 | + | ||
| 286 | + std::vector<uint64_t> offsets{0x1a4}; | ||
| 287 | + std::vector<SymbolizeResult> results; | ||
| 288 | + // 超时应返回 false;耗时约等于 SYMBOLIZER_TIMEOUT_MS(3s),不应挂到 sleep 30 结束。 | ||
| 289 | + const int64_t startMs = | ||
| 290 | + std::chrono::duration_cast<std::chrono::milliseconds>( | ||
| 291 | + std::chrono::steady_clock::now().time_since_epoch()).count(); | ||
| 292 | + EXPECT_FALSE(KernelSourceSymbolizer::Symbolize(oPath, offsets, results)); | ||
| 293 | + const int64_t elapsedMs = | ||
| 294 | + std::chrono::duration_cast<std::chrono::milliseconds>( | ||
| 295 | + std::chrono::steady_clock::now().time_since_epoch()).count() - startMs; | ||
| 296 | + // 明显早于假工具的 30s 睡眠即返回,说明超时 kill 生效(留足裕量,仅验证未被 sleep 拖住)。 | ||
| 297 | + EXPECT_LT(elapsedMs, 15000); | ||
| 298 | +} | ||
| 299 | + | ||
| 300 | +// 子进程先关闭 stdout(触发 read EOF),再长时间 sleep 且不退出。 | ||
| 301 | +// 验证:即便 poll 循环因 outEof 提前结束(timedOut=false),回收阶段也在宽限 deadline 内完成, | ||
| 302 | +// 不会卡在无界阻塞的 waitpid 上(zhangpengpeng8 检视点)。 | ||
| 303 | +TEST_F(KernelSourceSymbolizerUTest, SymbolizeReapsChildThatClosesStdoutButHangs) | ||
| 304 | +{ | ||
| 305 | + Tools::CaseWorkspace ws("SymbolizeReapsChildThatClosesStdoutButHangs"); | ||
| 306 | + const std::string oPath = ws.Root() + "/hang_after_eof.o"; | ||
| 307 | + WriteFile(oPath, MakeElfWithSection(true)); | ||
| 308 | + | ||
| 309 | + // 读走一行输入 -> 立刻 close stdout(exec 1>&-)-> sleep 很久不退出。 | ||
| 310 | + const std::string script = | ||
| 311 | + "#!/bin/sh\n" | ||
| 312 | + "read line\n" | ||
| 313 | + "exec 1>&-\n" | ||
| 314 | + "sleep 30\n"; | ||
| 315 | + const std::string tool = WriteFakeSymbolizer(ws.Root() + "/fake-symbolizer", script); | ||
| 316 | + (void)setenv("ADUMP_LLVM_SYMBOLIZER", tool.c_str(), 1); | ||
| 317 | + KernelSourceSymbolizer::ResetLocateCacheForTest(); | ||
| 318 | + | ||
| 319 | + std::vector<uint64_t> offsets{0x1a4}; | ||
| 320 | + std::vector<SymbolizeResult> results; | ||
| 321 | + const int64_t startMs = | ||
| 322 | + std::chrono::duration_cast<std::chrono::milliseconds>( | ||
| 323 | + std::chrono::steady_clock::now().time_since_epoch()).count(); | ||
| 324 | + // stdout EOF 后无有效输出,解析结果为空占位;关键在于调用要迅速返回、不被 sleep 30 拖住。 | ||
| 325 | + (void)KernelSourceSymbolizer::Symbolize(oPath, offsets, results); | ||
| 326 | + const int64_t elapsedMs = | ||
| 327 | + std::chrono::duration_cast<std::chrono::milliseconds>( | ||
| 328 | + std::chrono::steady_clock::now().time_since_epoch()).count() - startMs; | ||
| 329 | + // 远早于 30s:回收纳入宽限 deadline(SIGKILL 兜底),未被挂住的子进程拖死。 | ||
| 330 | + EXPECT_LT(elapsedMs, 15000); | ||
| 331 | +} | ||
| 332 | + | ||
| 333 | +// PrintClassificationSummary 聚类逻辑:不崩溃并覆盖分组分支。 | ||
| 334 | +TEST(KernelSymbolClassificationUTest, GroupsBySameOFileAndOffset) | ||
| 335 | +{ | ||
| 336 | + std::vector<ErrorLocation> locations; | ||
| 337 | + // 同一 .o、相同偏移的两个 core -> 归为一组。 | ||
| 338 | + ErrorLocation a; | ||
| 339 | + a.coreId = 2; a.coreType = 0; a.fixedPCOffset = 0x1a4; a.oFilePath = "add_host.o"; | ||
| 340 | + a.hasSymbol = true; a.symbolName = "add_kernel"; a.symbolOffset = 0x1c; | ||
| 341 | + a.src.ok = true; a.src.srcFile = "add.cpp"; a.src.srcLine = 88; a.src.srcColumn = 12; | ||
| 342 | + ErrorLocation b = a; | ||
| 343 | + b.coreId = 5; | ||
| 344 | + // 同一 .o、不同偏移 -> 另一组。 | ||
| 345 | + ErrorLocation c; | ||
| 346 | + c.coreId = 7; c.coreType = 1; c.fixedPCOffset = 0x3c; c.oFilePath = "add_host.o"; | ||
| 347 | + // 不同 .o -> 又一组。 | ||
| 348 | + ErrorLocation d; | ||
| 349 | + d.coreId = 9; d.coreType = 0; d.fixedPCOffset = 0x10; d.oFilePath = "mul_host.o"; | ||
| 350 | + locations = {a, b, c, d}; | ||
| 351 | + | ||
| 352 | + // 仅验证调用不崩溃(输出走日志)。 | ||
| 353 | + KernelSymbolLocator::PrintClassificationSummary(locations); | ||
| 354 | + KernelSymbolLocator::PrintClassificationSummary({}); | ||
| 355 | + SUCCEED(); | ||
| 356 | +} | ||
| 357 | + | ||
| 358 | +TEST(KernelSymbolResolveUTest, ResolveOFilePathFallsBackWhenNoDebugLine) | ||
| 359 | +{ | ||
| 360 | + Tools::CaseWorkspace ws("ResolveOFilePathFallsBackWhenNoDebugLine"); | ||
| 361 | + const std::string withPath = ws.Root() + "/with_debug.o"; | ||
| 362 | + const std::string withoutPath = ws.Root() + "/without_debug.o"; | ||
| 363 | + WriteFile(withPath, MakeElfWithSection(true)); | ||
| 364 | + WriteFile(withoutPath, MakeElfWithSection(false)); | ||
| 365 | + | ||
| 366 | + EXPECT_EQ(KernelSymbolLocator::ResolveOFilePath(withPath), withPath); | ||
| 367 | + EXPECT_TRUE(KernelSymbolLocator::ResolveOFilePath(withoutPath).empty()); | ||
| 368 | + EXPECT_TRUE(KernelSymbolLocator::ResolveOFilePath("").empty()); | ||
| 369 | +} | ||
| 370 | +} // namespace | ||
| @@ -455,15 +455,18 @@ TEST_F(KernelSymbolLocatorUTest, LocateAndPrintRejectsInvalidStateAndInputs) | |||
| 455 | { | 455 | { |
| 456 | KernelSymbolLocator locator; | 456 | KernelSymbolLocator locator; |
| 457 | ExceptionRegInfo regInfo{0U, nullptr}; | 457 | ExceptionRegInfo regInfo{0U, nullptr}; |
| 458 | - EXPECT_EQ(ADUMP_FAILED, locator.LocateAndPrintErrorSymbols(regInfo)); | 458 | + std::vector<ErrorLocation> locations; |
| 459 | - EXPECT_EQ(ADUMP_FAILED, locator.LocateAndPrintErrorSymbolsForCore(0U, RT_CORE_TYPE_AIC, regInfo)); | 459 | + ErrorLocation singleLocation; |
| 460 | + EXPECT_EQ(ADUMP_FAILED, locator.LocateErrorSymbols(regInfo, locations)); | ||
| 461 | + EXPECT_EQ(ADUMP_FAILED, locator.LocateErrorSymbolsForCore(0U, RT_CORE_TYPE_AIC, regInfo, singleLocation)); | ||
| 460 | 462 | ||
| 461 | ASSERT_EQ(ADUMP_SUCCESS, locator.InitFromBinBuffer(MakeElf(ValidSymbols()))); | 463 | ASSERT_EQ(ADUMP_SUCCESS, locator.InitFromBinBuffer(MakeElf(ValidSymbols()))); |
| 462 | - EXPECT_EQ(ADUMP_FAILED, locator.LocateAndPrintErrorSymbols(regInfo)); | 464 | + EXPECT_EQ(ADUMP_FAILED, locator.LocateErrorSymbols(regInfo, locations)); |
| 463 | 465 | ||
| 464 | rtExceptionErrRegInfo_t core = MakeCore(1U, RT_CORE_TYPE_AIC, 0x1000ULL, 0x1010ULL); | 466 | rtExceptionErrRegInfo_t core = MakeCore(1U, RT_CORE_TYPE_AIC, 0x1000ULL, 0x1010ULL); |
| 465 | ExceptionRegInfo singleCoreRegInfo{1U, &core}; | 467 | ExceptionRegInfo singleCoreRegInfo{1U, &core}; |
| 466 | - EXPECT_EQ(ADUMP_FAILED, locator.LocateAndPrintErrorSymbolsForCore(2U, RT_CORE_TYPE_AIC, singleCoreRegInfo)); | 468 | + EXPECT_EQ(ADUMP_FAILED, |
| 469 | + locator.LocateErrorSymbolsForCore(2U, RT_CORE_TYPE_AIC, singleCoreRegInfo, singleLocation)); | ||
| 467 | } | 470 | } |
| 468 | 471 | ||
| 469 | TEST_F(KernelSymbolLocatorUTest, LocateAndPrintCoversMatchedNoMatchedAndLowFixedPcBranches) | 472 | TEST_F(KernelSymbolLocatorUTest, LocateAndPrintCoversMatchedNoMatchedAndLowFixedPcBranches) |
| @@ -480,8 +483,10 @@ TEST_F(KernelSymbolLocatorUTest, LocateAndPrintCoversMatchedNoMatchedAndLowFixed | |||
| 480 | MakeCore(2U, RT_CORE_TYPE_AIC, 0x2000ULL, 0x1000ULL), | 483 | MakeCore(2U, RT_CORE_TYPE_AIC, 0x2000ULL, 0x1000ULL), |
| 481 | }; | 484 | }; |
| 482 | ExceptionRegInfo regInfo{3U, cores}; | 485 | ExceptionRegInfo regInfo{3U, cores}; |
| 483 | - EXPECT_EQ(ADUMP_SUCCESS, locator.LocateAndPrintErrorSymbols(regInfo)); | 486 | + std::vector<ErrorLocation> locations; |
| 484 | - EXPECT_EQ(ADUMP_SUCCESS, locator.LocateAndPrintErrorSymbolsForCore(1U, RT_CORE_TYPE_AIV, regInfo)); | 487 | + ErrorLocation singleLocation; |
| 488 | + EXPECT_EQ(ADUMP_SUCCESS, locator.LocateErrorSymbols(regInfo, locations)); | ||
| 489 | + EXPECT_EQ(ADUMP_SUCCESS, locator.LocateErrorSymbolsForCore(1U, RT_CORE_TYPE_AIV, regInfo, singleLocation)); | ||
| 485 | } | 490 | } |
| 486 | 491 | ||
| 487 | TEST_F(KernelSymbolLocatorUTest, LocateAndPrintNormalCloudV2) | 492 | TEST_F(KernelSymbolLocatorUTest, LocateAndPrintNormalCloudV2) |
| @@ -496,7 +501,8 @@ TEST_F(KernelSymbolLocatorUTest, LocateAndPrintNormalCloudV2) | |||
| 496 | core.errReg[RT_V100_AIC_ERR_0] = 1U << 9U; | 501 | core.errReg[RT_V100_AIC_ERR_0] = 1U << 9U; |
| 497 | core.errReg[RT_V100_CUBE_ERR_0] = 0U; | 502 | core.errReg[RT_V100_CUBE_ERR_0] = 0U; |
| 498 | ExceptionRegInfo regInfo{1U, &core}; | 503 | ExceptionRegInfo regInfo{1U, &core}; |
| 499 | - EXPECT_EQ(ADUMP_SUCCESS, locator.LocateAndPrintErrorSymbols(regInfo)); | 504 | + std::vector<ErrorLocation> locations; |
| 505 | + EXPECT_EQ(ADUMP_SUCCESS, locator.LocateErrorSymbols(regInfo, locations)); | ||
| 500 | } | 506 | } |
| 501 | 507 | ||
| 502 | TEST_F(KernelSymbolLocatorUTest, LocateAndPrintNormalCloudV4) | 508 | TEST_F(KernelSymbolLocatorUTest, LocateAndPrintNormalCloudV4) |
| @@ -511,7 +517,8 @@ TEST_F(KernelSymbolLocatorUTest, LocateAndPrintNormalCloudV4) | |||
| 511 | core.errReg[RT_V200_SU_ERROR_T0_0] = 1U; | 517 | core.errReg[RT_V200_SU_ERROR_T0_0] = 1U; |
| 512 | core.errReg[RT_V200_SU_ERR_INFO_T0_0] = 0U; | 518 | core.errReg[RT_V200_SU_ERR_INFO_T0_0] = 0U; |
| 513 | ExceptionRegInfo regInfo{1U, &core}; | 519 | ExceptionRegInfo regInfo{1U, &core}; |
| 514 | - EXPECT_EQ(ADUMP_SUCCESS, locator.LocateAndPrintErrorSymbols(regInfo)); | 520 | + std::vector<ErrorLocation> locations; |
| 521 | + EXPECT_EQ(ADUMP_SUCCESS, locator.LocateErrorSymbols(regInfo, locations)); | ||
| 515 | } | 522 | } |
| 516 | 523 | ||
| 517 | TEST_F(KernelSymbolLocatorUTest, StaticPcHelpersUseFactoryResult) | 524 | TEST_F(KernelSymbolLocatorUTest, StaticPcHelpersUseFactoryResult) |
| @@ -536,8 +543,9 @@ TEST_F(KernelSymbolLocatorUTest, DumpErrorSymbolsCoversFailureAndSuccessPaths) | |||
| 536 | rtExceptionErrRegInfo_t core = MakeCore(0U, RT_CORE_TYPE_AIC, 0ULL, 0ULL); | 543 | rtExceptionErrRegInfo_t core = MakeCore(0U, RT_CORE_TYPE_AIC, 0ULL, 0ULL); |
| 537 | ExceptionRegInfo regInfo{1U, &core}; | 544 | ExceptionRegInfo regInfo{1U, &core}; |
| 538 | 545 | ||
| 546 | + const std::string dumpPath = "./"; | ||
| 539 | MOCKER_CPP(&ExceptionInfoCommon::GetBinDataFromHandle).stubs().will(returnValue(ADUMP_FAILED)); | 547 | MOCKER_CPP(&ExceptionInfoCommon::GetBinDataFromHandle).stubs().will(returnValue(ADUMP_FAILED)); |
| 540 | - KernelSymbolLocator::DumpErrorSymbols(exception, regInfo); | 548 | + KernelSymbolLocator::DumpErrorSymbols(exception, regInfo, dumpPath); |
| 541 | GlobalMockObject::verify(); | 549 | GlobalMockObject::verify(); |
| 542 | 550 | ||
| 543 | g_mockElf = MakeElf(ValidSymbols()); | 551 | g_mockElf = MakeElf(ValidSymbols()); |
| @@ -546,9 +554,9 @@ TEST_F(KernelSymbolLocatorUTest, DumpErrorSymbolsCoversFailureAndSuccessPaths) | |||
| 546 | .will(invoke(MockGetBinDataFromHandle)); | 554 | .will(invoke(MockGetBinDataFromHandle)); |
| 547 | uint32_t v2Type = static_cast<uint32_t>(PlatformType::CHIP_CLOUD_V2); | 555 | uint32_t v2Type = static_cast<uint32_t>(PlatformType::CHIP_CLOUD_V2); |
| 548 | MOCKER_CPP(&Adx::AdumpDsmi::DrvGetPlatformType).stubs().with(outBound(v2Type)).will(returnValue(true)); | 556 | MOCKER_CPP(&Adx::AdumpDsmi::DrvGetPlatformType).stubs().with(outBound(v2Type)).will(returnValue(true)); |
| 549 | - KernelSymbolLocator::DumpErrorSymbols(exception, regInfo); | 557 | + KernelSymbolLocator::DumpErrorSymbols(exception, regInfo, dumpPath); |
| 550 | GlobalMockObject::verify(); | 558 | GlobalMockObject::verify(); |
| 551 | 559 | ||
| 552 | MOCKER_CPP(&ExceptionInfoCommon::GetExceptionRegInfo).stubs().will(returnValue(ADUMP_FAILED)); | 560 | MOCKER_CPP(&ExceptionInfoCommon::GetExceptionRegInfo).stubs().will(returnValue(ADUMP_FAILED)); |
| 553 | - KernelSymbolLocator::DumpErrorSymbols(exception); | 561 | + KernelSymbolLocator::DumpErrorSymbols(exception, dumpPath); |
| 554 | } | 562 | } |


级别:一般 问题:std::string(info_.kernelName) 未对 kernelName 为 nullptr 做防御 影响:若 info_.bin 非空但 kernelName 为 null,std::string(nullptr) 触发未定义行为,异常 dump 路径崩溃 修复建议:添加空指针保护,如 std::string(info_.kernelName != nullptr ? info_.kernelName : "")