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 2082 : ArgPtr TraceOpenLibrary(const char* libPath)
21 : {
22 2082 : if (libPath == NULL) {
23 0 : return NULL;
24 : }
25 :
26 2082 : void* handle = TraceDlopen(libPath, RTLD_LAZY);
27 2082 : if (handle == NULL) {
28 0 : return NULL;
29 : }
30 :
31 2082 : return handle;
32 : }
33 :
34 : /**
35 : * @brief dlclose library
36 : * @param [in] handle: library handle
37 : * @return TraStatus
38 : */
39 2072 : TraStatus TraceCloseLibrary(ArgPtr handle)
40 : {
41 2072 : if (handle == NULL) {
42 0 : return TRACE_INVALID_PARAM;
43 : }
44 :
45 2072 : if (TraceDlclose(handle) != 0) {
46 22 : return TRACE_FAILURE;
47 : }
48 :
49 2050 : 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 2082 : TraStatus TraceLoadFunc(ArgPtr handle, SymbolInfo* symbolInfos, uint32_t symbolNum)
60 : {
61 2082 : if ((handle == NULL) || (symbolInfos == NULL) || (symbolNum == 0)) {
62 0 : return TRACE_INVALID_PARAM;
63 : }
64 :
65 6246 : for (uint32_t i = 0; i < symbolNum; i++) {
66 4164 : const char* symbol = symbolInfos[i].symbol;
67 4164 : if (symbol == NULL) {
68 0 : continue;
69 : }
70 4164 : void* value = TraceDlsym(handle, symbol);
71 4164 : if (value == NULL) {
72 0 : ADIAG_WAR("can not find function\"%s\" symbol.", symbol);
73 0 : continue;
74 : }
75 4164 : symbolInfos[i].handle = value;
76 : }
77 :
78 2082 : return TRACE_SUCCESS;
79 : }
|