Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 :
11 : #include "scd_map.h"
12 : #include "scd_log.h"
13 : #include "adiag_utils.h"
14 :
15 16 : uintptr_t ScdMapGetRelPc(ScdMap* map, uintptr_t absPc)
16 : {
17 16 : return absPc - (map->start - map->offset) + map->dl.elf.memory->data + map->dl.elf.loadBias;
18 : }
19 :
20 12 : uintptr_t ScdMapGetBase(ScdMap* map) { return map->start - (uintptr_t)map->offset; }
21 :
22 : /**
23 : * @brief update map node based on start addr and end addr
24 : * @param [in] map: map node
25 : * @param [in] start: start addr
26 : * @param [in] end: end addr
27 : * @param [in] offset: offset
28 : * @return NA
29 : */
30 6388 : TraStatus ScdMapUpdata(ScdMap* map, uintptr_t start, uintptr_t end, size_t offset)
31 : {
32 6388 : SCD_CHK_PTR_ACTION(map, return TRACE_FAILURE);
33 6388 : if ((map->end != start) && (map->start != end)) {
34 52 : return TRACE_FAILURE;
35 : }
36 :
37 6336 : map->start = SCD_MIN(start, map->start);
38 6336 : map->end = SCD_MAX(end, map->end);
39 6336 : map->offset = SCD_MIN(offset, map->offset);
40 6336 : return TRACE_SUCCESS;
41 : }
42 :
43 1111 : STATIC TraStatus ScdMapInit(ScdMap* map, uintptr_t start, uintptr_t end, size_t offset, const char* name)
44 : {
45 : // obtain the data from the file.
46 : // fault tolerance processing will be added later : if failed from the file, use the ptrace to obtain the data
47 1111 : SCD_CHK_PTR_ACTION(name, return TRACE_FAILURE);
48 :
49 : // map
50 1111 : map->name = strdup(name);
51 1111 : SCD_CHK_PTR_ACTION(map->name, return TRACE_FAILURE);
52 :
53 1111 : map->nameLength = strlen(name);
54 1111 : map->start = start;
55 1111 : map->end = end;
56 1111 : map->offset = offset;
57 :
58 : // dl
59 1111 : TraStatus ret = ScdDlInit(&map->dl);
60 1111 : if (ret != TRACE_SUCCESS) {
61 18 : SCD_DLOG_ERR("dl[%s] init failed.", name);
62 18 : ADIAG_SAFE_FREE(map->name);
63 18 : return TRACE_FAILURE;
64 : }
65 1093 : return TRACE_SUCCESS;
66 : }
67 :
68 1093 : STATIC void ScdMapUninit(ScdMap* map)
69 : {
70 1093 : SCD_CHK_PTR_ACTION(map, return);
71 1093 : ScdDlUninit(&map->dl);
72 1093 : ADIAG_SAFE_FREE(map->name);
73 : }
74 :
75 : /**
76 : * @brief create map node
77 : * @param [in] start: start addr
78 : * @param [in] end: end addr
79 : * @param [in] offset: offset
80 : * @param [in] name: dynamic library name
81 : * @return ScdMap *
82 : */
83 1111 : ScdMap* ScdMapCreate(uintptr_t start, uintptr_t end, size_t offset, const char* name)
84 : {
85 1111 : ScdMap* map = (ScdMap*)AdiagMalloc(sizeof(ScdMap));
86 1111 : if (map != NULL) {
87 1111 : TraStatus ret = ScdMapInit(map, start, end, offset, name);
88 1111 : if (ret != TRACE_SUCCESS) {
89 18 : SCD_DLOG_ERR("init map in %s failed, ret = %d", name, ret);
90 18 : ADIAG_SAFE_FREE(map);
91 : }
92 : }
93 1111 : return map;
94 : }
95 :
96 : /**
97 : * @brief destroy a map node
98 : * @param [in] map: a map node
99 : * @return NA
100 : */
101 1093 : void ScdMapDestroy(ScdMap** map)
102 : {
103 1093 : SCD_CHK_PTR_ACTION(map, return);
104 1093 : ScdMapUninit(*map);
105 1093 : ADIAG_SAFE_FREE(*map);
106 : }
|