1  /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef __DFX_TRACE_DLSYM_H__
16 #define __DFX_TRACE_DLSYM_H__
17 
18 #include <cstdlib>
19 #include <cstdint>
20 namespace OHOS {
21 namespace HiviewDFX {
22 void DfxEnableTraceDlsym(bool enableTrace);
23 #ifdef DFX_ENABLE_TRACE
24 void DfxStartTraceDlsym(const char* value);
25 void DfxFinishTraceDlsym();
26 
27 class DfxTraceScoped {
28 public:
DfxTraceScoped(const char * value)29     DfxTraceScoped(const char* value)
30     {
31         DfxStartTraceDlsym(value);
32     }
33     DfxTraceScoped(const DfxTraceScoped&) = delete;
34     DfxTraceScoped& operator=(const DfxTraceScoped&) = delete;
35 
~DfxTraceScoped()36     ~DfxTraceScoped()
37     {
38         DfxFinishTraceDlsym();
39     }
40 };
41 constexpr int TRACE_BUF_LEN = 128;
42 void DfxStartTraceDlsymFormat(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
43 void DfxStartTraceDlsym(const char* value);
44 void DfxFinishTraceDlsym();
45 void FormatTraceName(char *name, size_t size, const char *fmt, ...) __attribute__((format(printf, 3, 4)));
46 
47 #define DFX_TRACE_SCOPED_DLSYM(fmt, ...) \
48     char traceName[TRACE_BUF_LEN] = {0}; \
49     FormatTraceName(traceName, TRACE_BUF_LEN, fmt, ##__VA_ARGS__); \
50     DfxTraceScoped trace(traceName)
51 
52 #define DFX_TRACE_START_DLSYM(fmt, ...) DfxStartTraceDlsymFormat(fmt, ##__VA_ARGS__)
53 #define DFX_TRACE_FINISH_DLSYM(...) DfxFinishTraceDlsym()
54 #else
55 #define DFX_TRACE_SCOPED_DLSYM(fmt, ...)
56 #define DFX_TRACE_START_DLSYM(fmt, ...)
57 #define DFX_TRACE_FINISH_DLSYM(...)
58 #endif
59 } // namespace HiviewDFX
60 } // namespace OHOS
61 #endif