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,
24 : TRACE_FILE_MODE);
25 : if (fd == -1) {
26 : return;
27 : }
28 : write(fd, (void *)addr, size);
29 : close(fd);
30 : fd = -1;
31 : }
32 : #endif
33 :
34 16 : void DumpStackToFile(uintptr_t addr, size_t size)
35 : {
36 : #ifndef ENABLE_DUMP_DFX_INFO
37 : (void)addr;
38 : (void)size;
39 : #else
40 : DumpToFile(TMP_DUMP_STACK_PATH, addr, size);
41 : #endif
42 16 : }
43 :
44 0 : void DumpMemory(uintptr_t addr, size_t size)
45 : {
46 : #ifndef ENABLE_DUMP_DFX_INFO
47 : (void)addr;
48 : (void)size;
49 : #else
50 : static size_t cnt = 0;
51 : static uintptr_t addrList[20] = {0};
52 : for (size_t i = 0; i < cnt; i++) {
53 : if (addr == addrList[i]) {
54 : return;
55 : }
56 : }
57 : addrList[cnt] = addr;
58 : cnt++;
59 : char filePath[MAX_FILE_PATH_LENGTH];
60 : int32_t ret = sprintf_s(filePath, MAX_FILE_PATH_LENGTH, TMP_DUMP_ELF_PATH, cnt);
61 : if (ret == -1) {
62 : LOGE("snprintf_s elf path failed, strerr=%s", strerror(AdiagGetErrorCode()));
63 : return;
64 : }
65 : DumpToFile(filePath, addr, size);
66 : LOGI("[LLT] write elf to %s , size : %zu", filePath, size);
67 : #endif
68 0 : }
|