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 "trace_load_library.h"
12 : #include "adiag_print.h"
13 : #include "trace_system_api.h"
14 :
15 : /**
16 : * @brief dlopen library
17 : * @param [in] libPath: library path
18 : * @return handle
19 : */
20 1650 : ArgPtr TraceOpenLibrary(const char *libPath)
21 : {
22 1650 : if (libPath == NULL) {
23 0 : return NULL;
24 : }
25 :
26 1650 : void *handle = TraceDlopen(libPath, RTLD_LAZY);
27 1650 : if (handle == NULL) {
28 0 : return NULL;
29 : }
30 :
31 1650 : return handle;
32 : }
33 :
34 : /**
35 : * @brief dlclose library
36 : * @param [in] handle: library handle
37 : * @return TraStatus
38 : */
39 1620 : TraStatus TraceCloseLibrary(ArgPtr handle)
40 : {
41 1620 : if (handle == NULL) {
42 0 : return TRACE_INVALID_PARAM;
43 : }
44 :
45 1620 : if (TraceDlclose(handle) != 0) {
46 20 : return TRACE_FAILURE;
47 : }
48 :
49 1600 : return TRACE_SUCCESS;
50 : }
51 :
52 : /**
53 : * @brief load function symbol
54 : * @param [in] handle: library handle
55 : * @param [out] symbolInfos: symbol info and handle
56 : * @param [in] symbolNum: symbol nums
57 : * @return TraStatus
58 : */
59 1650 : TraStatus TraceLoadFunc(ArgPtr handle, SymbolInfo *symbolInfos, uint32_t symbolNum)
60 : {
61 1650 : if ((handle == NULL) || (symbolInfos == NULL) || (symbolNum == 0)) {
62 0 : return TRACE_INVALID_PARAM;
63 : }
64 :
65 4950 : for (uint32_t i = 0; i < symbolNum; i++) {
66 3300 : const char *symbol = symbolInfos[i].symbol;
67 3300 : if (symbol == NULL) {
68 0 : continue;
69 : }
70 3300 : void *value = TraceDlsym(handle, symbol);
71 3300 : if (value == NULL) {
72 0 : ADIAG_WAR("can not find function\"%s\" symbol.", symbol);
73 0 : continue;
74 : }
75 3300 : symbolInfos[i].handle = value;
76 : }
77 :
78 1650 : return TRACE_SUCCESS;
79 : }
|