Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 : #include "stacktrace_unwind_dfx.h"
11 : #include <fcntl.h>
12 : #include "stacktrace_logger.h"
13 : #include "trace_system_api.h"
14 : #include "adiag_utils.h"
15 :
16 : #define TMP_DUMP_STACK_PATH "/tmp/stack.bin"
17 : #define TMP_DUMP_ELF_PATH "/tmp/elf_%zu.bin"
18 : #define MAX_FILE_PATH_LENGTH 100U
19 : #ifdef ENABLE_DUMP_DFX_INFO
20 : static void DumpToFile(const char* filePath, uintptr_t addr, size_t size)
21 : {
22 : #define TRACE_FILE_MODE 0640U
23 : int32_t fd = TraceOpen(filePath, (uint32_t)O_CREAT | (uint32_t)O_WRONLY | (uint32_t)O_APPEND, TRACE_FILE_MODE);
24 : if (fd == -1) {
25 : return;
26 : }
27 : write(fd, (void*)addr, size);
28 : close(fd);
29 : fd = -1;
30 : }
31 : #endif
32 :
33 18 : void DumpStackToFile(uintptr_t addr, size_t size)
34 : {
35 : #ifndef ENABLE_DUMP_DFX_INFO
36 : (void)addr;
37 : (void)size;
38 : #else
39 : DumpToFile(TMP_DUMP_STACK_PATH, addr, size);
40 : #endif
41 18 : }
42 :
43 0 : void DumpMemory(uintptr_t addr, size_t size)
44 : {
45 : #ifndef ENABLE_DUMP_DFX_INFO
46 : (void)addr;
47 : (void)size;
48 : #else
49 : static size_t cnt = 0;
50 : static uintptr_t addrList[20] = {0};
51 : for (size_t i = 0; i < cnt; i++) {
52 : if (addr == addrList[i]) {
53 : return;
54 : }
55 : }
56 : addrList[cnt] = addr;
57 : cnt++;
58 : char filePath[MAX_FILE_PATH_LENGTH];
59 : int32_t ret = sprintf_s(filePath, MAX_FILE_PATH_LENGTH, TMP_DUMP_ELF_PATH, cnt);
60 : if (ret == -1) {
61 : LOGE("snprintf_s elf path failed, strerr=%s", strerror(AdiagGetErrorCode()));
62 : return;
63 : }
64 : DumpToFile(filePath, addr, size);
65 : LOGI("[LLT] write elf to %s , size : %zu", filePath, size);
66 : #endif
67 0 : }
|