xref: /freebsd-src/contrib/llvm-project/openmp/runtime/src/thirdparty/ittnotify/ittnotify_static.cpp (revision 349cc55c9796c4596a5b9904cd3281af295f878f)
1489b1cf2SDimitry Andric 
2489b1cf2SDimitry Andric //===----------------------------------------------------------------------===//
3489b1cf2SDimitry Andric //
4489b1cf2SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5489b1cf2SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
6489b1cf2SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7489b1cf2SDimitry Andric //
8489b1cf2SDimitry Andric //===----------------------------------------------------------------------===//
9489b1cf2SDimitry Andric 
10*349cc55cSDimitry Andric #include "kmp_config.h" // INTEL_ITTNOTIFY_PREFIX definition
11489b1cf2SDimitry Andric #include "ittnotify_config.h"
12489b1cf2SDimitry Andric 
13489b1cf2SDimitry Andric #if ITT_PLATFORM == ITT_PLATFORM_WIN
14*349cc55cSDimitry Andric #if !defined(PATH_MAX)
15489b1cf2SDimitry Andric #define PATH_MAX 512
16489b1cf2SDimitry Andric #endif
17489b1cf2SDimitry Andric #else /* ITT_PLATFORM!=ITT_PLATFORM_WIN */
18489b1cf2SDimitry Andric #include <limits.h>
19489b1cf2SDimitry Andric #include <dlfcn.h>
20489b1cf2SDimitry Andric #include <errno.h>
21489b1cf2SDimitry Andric #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
22489b1cf2SDimitry Andric #include <stdio.h>
23489b1cf2SDimitry Andric #include <stdlib.h>
24489b1cf2SDimitry Andric #include <stdarg.h>
25489b1cf2SDimitry Andric #include <string.h>
26489b1cf2SDimitry Andric 
27489b1cf2SDimitry Andric #define INTEL_NO_MACRO_BODY
28489b1cf2SDimitry Andric #define INTEL_ITTNOTIFY_API_PRIVATE
29489b1cf2SDimitry Andric #include "ittnotify.h"
30489b1cf2SDimitry Andric #include "legacy/ittnotify.h"
31489b1cf2SDimitry Andric 
32489b1cf2SDimitry Andric #include "disable_warnings.h"
33489b1cf2SDimitry Andric 
34*349cc55cSDimitry Andric static const char api_version[] = API_VERSION "\0\n@(#) $Revision$\n";
35489b1cf2SDimitry Andric 
36489b1cf2SDimitry Andric #define _N_(n) ITT_JOIN(INTEL_ITTNOTIFY_PREFIX, n)
37489b1cf2SDimitry Andric 
38*349cc55cSDimitry Andric #ifndef HAS_CPP_ATTR
39*349cc55cSDimitry Andric #if defined(__cplusplus) && defined(__has_cpp_attribute)
40*349cc55cSDimitry Andric #define HAS_CPP_ATTR(X) __has_cpp_attribute(X)
41*349cc55cSDimitry Andric #else
42*349cc55cSDimitry Andric #define HAS_CPP_ATTR(X) 0
43*349cc55cSDimitry Andric #endif
44*349cc55cSDimitry Andric #endif
45*349cc55cSDimitry Andric 
46*349cc55cSDimitry Andric #ifndef HAS_C_ATTR
47*349cc55cSDimitry Andric #if defined(__STDC__) && defined(__has_c_attribute)
48*349cc55cSDimitry Andric #define HAS_C_ATTR(X) __has_c_attribute(X)
49*349cc55cSDimitry Andric #else
50*349cc55cSDimitry Andric #define HAS_C_ATTR(X) 0
51*349cc55cSDimitry Andric #endif
52*349cc55cSDimitry Andric #endif
53*349cc55cSDimitry Andric 
54*349cc55cSDimitry Andric #ifndef HAS_GNU_ATTR
55*349cc55cSDimitry Andric #if defined(__has_attribute)
56*349cc55cSDimitry Andric #define HAS_GNU_ATTR(X) __has_attribute(X)
57*349cc55cSDimitry Andric #else
58*349cc55cSDimitry Andric #define HAS_GNU_ATTR(X) 0
59*349cc55cSDimitry Andric #endif
60*349cc55cSDimitry Andric #endif
61*349cc55cSDimitry Andric 
62*349cc55cSDimitry Andric #ifndef ITT_ATTRIBUTE_FALLTHROUGH
63*349cc55cSDimitry Andric #if (HAS_CPP_ATTR(fallthrough) || HAS_C_ATTR(fallthrough)) &&                  \
64*349cc55cSDimitry Andric     (__cplusplus >= 201703L || _MSVC_LANG >= 201703L)
65*349cc55cSDimitry Andric #define ITT_ATTRIBUTE_FALLTHROUGH [[fallthrough]]
66*349cc55cSDimitry Andric #elif HAS_CPP_ATTR(gnu::fallthrough)
67*349cc55cSDimitry Andric #define ITT_ATTRIBUTE_FALLTHROUGH [[gnu::fallthrough]]
68*349cc55cSDimitry Andric #elif HAS_CPP_ATTR(clang::fallthrough)
69*349cc55cSDimitry Andric #define ITT_ATTRIBUTE_FALLTHROUGH [[clang::fallthrough]]
70*349cc55cSDimitry Andric #elif HAS_GNU_ATTR(fallthrough) && !__INTEL_COMPILER
71*349cc55cSDimitry Andric #define ITT_ATTRIBUTE_FALLTHROUGH __attribute__((fallthrough))
72*349cc55cSDimitry Andric #else
73*349cc55cSDimitry Andric #define ITT_ATTRIBUTE_FALLTHROUGH
74*349cc55cSDimitry Andric #endif
75*349cc55cSDimitry Andric #endif
76*349cc55cSDimitry Andric 
77489b1cf2SDimitry Andric #if ITT_OS == ITT_OS_WIN
78489b1cf2SDimitry Andric static const char *ittnotify_lib_name = "libittnotify.dll";
79489b1cf2SDimitry Andric #elif ITT_OS == ITT_OS_LINUX || ITT_OS == ITT_OS_FREEBSD
80489b1cf2SDimitry Andric static const char *ittnotify_lib_name = "libittnotify.so";
81489b1cf2SDimitry Andric #elif ITT_OS == ITT_OS_MAC
82489b1cf2SDimitry Andric static const char *ittnotify_lib_name = "libittnotify.dylib";
83489b1cf2SDimitry Andric #else
84489b1cf2SDimitry Andric #error Unsupported or unknown OS.
85489b1cf2SDimitry Andric #endif
86489b1cf2SDimitry Andric 
87489b1cf2SDimitry Andric #ifdef __ANDROID__
88489b1cf2SDimitry Andric #include <android/log.h>
89489b1cf2SDimitry Andric #include <stdio.h>
90489b1cf2SDimitry Andric #include <unistd.h>
91489b1cf2SDimitry Andric #include <sys/types.h>
92489b1cf2SDimitry Andric #include <sys/stat.h>
93489b1cf2SDimitry Andric #include <fcntl.h>
94489b1cf2SDimitry Andric #include <linux/limits.h>
95489b1cf2SDimitry Andric 
96489b1cf2SDimitry Andric #ifdef ITT_ANDROID_LOG
97489b1cf2SDimitry Andric #define ITT_ANDROID_LOG_TAG "INTEL_VTUNE_USERAPI"
98fe6060f1SDimitry Andric #define ITT_ANDROID_LOGI(...)                                                  \
99fe6060f1SDimitry Andric   ((void)__android_log_print(ANDROID_LOG_INFO, ITT_ANDROID_LOG_TAG,            \
100fe6060f1SDimitry Andric                              __VA_ARGS__))
101fe6060f1SDimitry Andric #define ITT_ANDROID_LOGW(...)                                                  \
102fe6060f1SDimitry Andric   ((void)__android_log_print(ANDROID_LOG_WARN, ITT_ANDROID_LOG_TAG,            \
103fe6060f1SDimitry Andric                              __VA_ARGS__))
104fe6060f1SDimitry Andric #define ITT_ANDROID_LOGE(...)                                                  \
105fe6060f1SDimitry Andric   ((void)__android_log_print(ANDROID_LOG_ERROR, ITT_ANDROID_LOG_TAG,           \
106fe6060f1SDimitry Andric                              __VA_ARGS__))
107fe6060f1SDimitry Andric #define ITT_ANDROID_LOGD(...)                                                  \
108fe6060f1SDimitry Andric   ((void)__android_log_print(ANDROID_LOG_DEBUG, ITT_ANDROID_LOG_TAG,           \
109fe6060f1SDimitry Andric                              __VA_ARGS__))
110489b1cf2SDimitry Andric #else
111489b1cf2SDimitry Andric #define ITT_ANDROID_LOGI(...)
112489b1cf2SDimitry Andric #define ITT_ANDROID_LOGW(...)
113489b1cf2SDimitry Andric #define ITT_ANDROID_LOGE(...)
114489b1cf2SDimitry Andric #define ITT_ANDROID_LOGD(...)
115489b1cf2SDimitry Andric #endif
116489b1cf2SDimitry Andric 
117489b1cf2SDimitry Andric /* default location of userapi collector on Android */
118fe6060f1SDimitry Andric #define ANDROID_ITTNOTIFY_DEFAULT_PATH_MASK(x)                                 \
119fe6060f1SDimitry Andric   "/data/data/com.intel.vtune/perfrun/lib" #x "/runtime/libittnotify.so"
120489b1cf2SDimitry Andric 
121489b1cf2SDimitry Andric #if ITT_ARCH == ITT_ARCH_IA32 || ITT_ARCH == ITT_ARCH_ARM
122489b1cf2SDimitry Andric #define ANDROID_ITTNOTIFY_DEFAULT_PATH ANDROID_ITTNOTIFY_DEFAULT_PATH_MASK(32)
123489b1cf2SDimitry Andric #else
124489b1cf2SDimitry Andric #define ANDROID_ITTNOTIFY_DEFAULT_PATH ANDROID_ITTNOTIFY_DEFAULT_PATH_MASK(64)
125489b1cf2SDimitry Andric #endif
126489b1cf2SDimitry Andric 
127489b1cf2SDimitry Andric #endif
128489b1cf2SDimitry Andric 
129489b1cf2SDimitry Andric #ifndef LIB_VAR_NAME
130*349cc55cSDimitry Andric #if ITT_ARCH == ITT_ARCH_IA32 || ITT_ARCH == ITT_ARCH_ARM
131489b1cf2SDimitry Andric #define LIB_VAR_NAME INTEL_LIBITTNOTIFY32
132489b1cf2SDimitry Andric #else
133489b1cf2SDimitry Andric #define LIB_VAR_NAME INTEL_LIBITTNOTIFY64
134489b1cf2SDimitry Andric #endif
135489b1cf2SDimitry Andric #endif /* LIB_VAR_NAME */
136489b1cf2SDimitry Andric 
137fe6060f1SDimitry Andric #define ITT_MUTEX_INIT_AND_LOCK(p)                                             \
138489b1cf2SDimitry Andric   {                                                                            \
139fe6060f1SDimitry Andric     if (PTHREAD_SYMBOLS) {                                                     \
140fe6060f1SDimitry Andric       if (!p.mutex_initialized) {                                              \
141fe6060f1SDimitry Andric         if (__itt_interlocked_increment(&p.atomic_counter) == 1) {             \
142489b1cf2SDimitry Andric           __itt_mutex_init(&p.mutex);                                          \
143489b1cf2SDimitry Andric           p.mutex_initialized = 1;                                             \
144fe6060f1SDimitry Andric         } else                                                                 \
145489b1cf2SDimitry Andric           while (!p.mutex_initialized)                                         \
146489b1cf2SDimitry Andric             __itt_thread_yield();                                              \
147489b1cf2SDimitry Andric       }                                                                        \
148489b1cf2SDimitry Andric       __itt_mutex_lock(&p.mutex);                                              \
149489b1cf2SDimitry Andric     }                                                                          \
150489b1cf2SDimitry Andric   }
151489b1cf2SDimitry Andric 
152*349cc55cSDimitry Andric #define ITT_MODULE_OBJECT_VERSION 1
153*349cc55cSDimitry Andric 
154489b1cf2SDimitry Andric typedef int(__itt_init_ittlib_t)(const char *, __itt_group_id);
155489b1cf2SDimitry Andric 
156489b1cf2SDimitry Andric /* this define used to control initialization function name. */
157489b1cf2SDimitry Andric #ifndef __itt_init_ittlib_name
158489b1cf2SDimitry Andric ITT_EXTERN_C int _N_(init_ittlib)(const char *, __itt_group_id);
159489b1cf2SDimitry Andric static __itt_init_ittlib_t *__itt_init_ittlib_ptr = _N_(init_ittlib);
160489b1cf2SDimitry Andric #define __itt_init_ittlib_name __itt_init_ittlib_ptr
161489b1cf2SDimitry Andric #endif /* __itt_init_ittlib_name */
162489b1cf2SDimitry Andric 
163489b1cf2SDimitry Andric typedef void(__itt_fini_ittlib_t)(void);
164489b1cf2SDimitry Andric 
165489b1cf2SDimitry Andric /* this define used to control finalization function name. */
166489b1cf2SDimitry Andric #ifndef __itt_fini_ittlib_name
167489b1cf2SDimitry Andric ITT_EXTERN_C void _N_(fini_ittlib)(void);
168489b1cf2SDimitry Andric static __itt_fini_ittlib_t *__itt_fini_ittlib_ptr = _N_(fini_ittlib);
169489b1cf2SDimitry Andric #define __itt_fini_ittlib_name __itt_fini_ittlib_ptr
170489b1cf2SDimitry Andric #endif /* __itt_fini_ittlib_name */
171489b1cf2SDimitry Andric 
172*349cc55cSDimitry Andric extern __itt_global _N_(_ittapi_global);
173*349cc55cSDimitry Andric 
174489b1cf2SDimitry Andric /* building pointers to imported funcs */
175489b1cf2SDimitry Andric #undef ITT_STUBV
176489b1cf2SDimitry Andric #undef ITT_STUB
177489b1cf2SDimitry Andric #define ITT_STUB(api, type, name, args, params, ptr, group, format)            \
178489b1cf2SDimitry Andric   static type api ITT_VERSIONIZE(ITT_JOIN(_N_(name), _init)) args;             \
179489b1cf2SDimitry Andric   typedef type api ITT_JOIN(_N_(name), _t) args;                               \
180fe6060f1SDimitry Andric   ITT_EXTERN_C_BEGIN ITT_JOIN(_N_(name), _t) * ITTNOTIFY_NAME(name) =          \
181fe6060f1SDimitry Andric       ITT_VERSIONIZE(ITT_JOIN(_N_(name), _init));                              \
182fe6060f1SDimitry Andric   ITT_EXTERN_C_END                                                             \
183fe6060f1SDimitry Andric   static type api ITT_VERSIONIZE(ITT_JOIN(_N_(name), _init)) args {            \
184*349cc55cSDimitry Andric     if (!_N_(_ittapi_global).api_initialized &&                                \
185*349cc55cSDimitry Andric         _N_(_ittapi_global).thread_list == NULL)                               \
186489b1cf2SDimitry Andric       __itt_init_ittlib_name(NULL, __itt_group_all);                           \
187fe6060f1SDimitry Andric     if (ITTNOTIFY_NAME(name) &&                                                \
188fe6060f1SDimitry Andric         ITTNOTIFY_NAME(name) != ITT_VERSIONIZE(ITT_JOIN(_N_(name), _init)))    \
189489b1cf2SDimitry Andric       return ITTNOTIFY_NAME(name) params;                                      \
190489b1cf2SDimitry Andric     else                                                                       \
191489b1cf2SDimitry Andric       return (type)0;                                                          \
192489b1cf2SDimitry Andric   }
193489b1cf2SDimitry Andric 
194489b1cf2SDimitry Andric #define ITT_STUBV(api, type, name, args, params, ptr, group, format)           \
195489b1cf2SDimitry Andric   static type api ITT_VERSIONIZE(ITT_JOIN(_N_(name), _init)) args;             \
196489b1cf2SDimitry Andric   typedef type api ITT_JOIN(_N_(name), _t) args;                               \
197fe6060f1SDimitry Andric   ITT_EXTERN_C_BEGIN ITT_JOIN(_N_(name), _t) * ITTNOTIFY_NAME(name) =          \
198fe6060f1SDimitry Andric       ITT_VERSIONIZE(ITT_JOIN(_N_(name), _init));                              \
199fe6060f1SDimitry Andric   ITT_EXTERN_C_END                                                             \
200fe6060f1SDimitry Andric   static type api ITT_VERSIONIZE(ITT_JOIN(_N_(name), _init)) args {            \
201*349cc55cSDimitry Andric     if (!_N_(_ittapi_global).api_initialized &&                                \
202*349cc55cSDimitry Andric         _N_(_ittapi_global).thread_list == NULL)                               \
203489b1cf2SDimitry Andric       __itt_init_ittlib_name(NULL, __itt_group_all);                           \
204fe6060f1SDimitry Andric     if (ITTNOTIFY_NAME(name) &&                                                \
205fe6060f1SDimitry Andric         ITTNOTIFY_NAME(name) != ITT_VERSIONIZE(ITT_JOIN(_N_(name), _init)))    \
206489b1cf2SDimitry Andric       ITTNOTIFY_NAME(name) params;                                             \
207489b1cf2SDimitry Andric     else                                                                       \
208489b1cf2SDimitry Andric       return;                                                                  \
209489b1cf2SDimitry Andric   }
210489b1cf2SDimitry Andric 
211489b1cf2SDimitry Andric #undef __ITT_INTERNAL_INIT
212489b1cf2SDimitry Andric #include "ittnotify_static.h"
213489b1cf2SDimitry Andric 
214489b1cf2SDimitry Andric #undef ITT_STUB
215489b1cf2SDimitry Andric #undef ITT_STUBV
216489b1cf2SDimitry Andric #define ITT_STUB(api, type, name, args, params, ptr, group, format)            \
217489b1cf2SDimitry Andric   static type api ITT_VERSIONIZE(ITT_JOIN(_N_(name), _init)) args;             \
218489b1cf2SDimitry Andric   typedef type api ITT_JOIN(_N_(name), _t) args;                               \
219fe6060f1SDimitry Andric   ITT_EXTERN_C_BEGIN ITT_JOIN(_N_(name), _t) * ITTNOTIFY_NAME(name) =          \
220fe6060f1SDimitry Andric       ITT_VERSIONIZE(ITT_JOIN(_N_(name), _init));                              \
221fe6060f1SDimitry Andric   ITT_EXTERN_C_END
222489b1cf2SDimitry Andric 
223489b1cf2SDimitry Andric #define ITT_STUBV(api, type, name, args, params, ptr, group, format)           \
224489b1cf2SDimitry Andric   static type api ITT_VERSIONIZE(ITT_JOIN(_N_(name), _init)) args;             \
225489b1cf2SDimitry Andric   typedef type api ITT_JOIN(_N_(name), _t) args;                               \
226fe6060f1SDimitry Andric   ITT_EXTERN_C_BEGIN ITT_JOIN(_N_(name), _t) * ITTNOTIFY_NAME(name) =          \
227fe6060f1SDimitry Andric       ITT_VERSIONIZE(ITT_JOIN(_N_(name), _init));                              \
228fe6060f1SDimitry Andric   ITT_EXTERN_C_END
229489b1cf2SDimitry Andric 
230489b1cf2SDimitry Andric #define __ITT_INTERNAL_INIT
231489b1cf2SDimitry Andric #include "ittnotify_static.h"
232489b1cf2SDimitry Andric #undef __ITT_INTERNAL_INIT
233489b1cf2SDimitry Andric 
234489b1cf2SDimitry Andric ITT_GROUP_LIST(group_list);
235489b1cf2SDimitry Andric 
236489b1cf2SDimitry Andric #pragma pack(push, 8)
237489b1cf2SDimitry Andric 
238fe6060f1SDimitry Andric typedef struct ___itt_group_alias {
239489b1cf2SDimitry Andric   const char *env_var;
240489b1cf2SDimitry Andric   __itt_group_id groups;
241489b1cf2SDimitry Andric } __itt_group_alias;
242489b1cf2SDimitry Andric 
243489b1cf2SDimitry Andric static __itt_group_alias group_alias[] = {
244fe6060f1SDimitry Andric     {"KMP_FOR_TPROFILE",
245fe6060f1SDimitry Andric      (__itt_group_id)(__itt_group_control | __itt_group_thread |
246fe6060f1SDimitry Andric                       __itt_group_sync | __itt_group_mark)},
247fe6060f1SDimitry Andric     {"KMP_FOR_TCHECK",
248fe6060f1SDimitry Andric      (__itt_group_id)(__itt_group_control | __itt_group_thread |
249fe6060f1SDimitry Andric                       __itt_group_sync | __itt_group_fsync | __itt_group_mark |
250fe6060f1SDimitry Andric                       __itt_group_suppress)},
251489b1cf2SDimitry Andric     {NULL, (__itt_group_none)},
252fe6060f1SDimitry Andric     {api_version,
253fe6060f1SDimitry Andric      (__itt_group_none)} /* !!! Just to avoid unused code elimination !!! */
254489b1cf2SDimitry Andric };
255489b1cf2SDimitry Andric 
256489b1cf2SDimitry Andric #pragma pack(pop)
257489b1cf2SDimitry Andric 
258*349cc55cSDimitry Andric #if ITT_PLATFORM == ITT_PLATFORM_WIN
259*349cc55cSDimitry Andric #if _MSC_VER
260489b1cf2SDimitry Andric #pragma warning(push)
261*349cc55cSDimitry Andric #pragma warning(disable : 4054) /* warning C4054: 'type cast' : from function  \
262fe6060f1SDimitry Andric                                    pointer 'XXX' to data pointer 'void *' */
263*349cc55cSDimitry Andric #endif
264489b1cf2SDimitry Andric #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
265489b1cf2SDimitry Andric 
266489b1cf2SDimitry Andric static __itt_api_info api_list[] = {
267489b1cf2SDimitry Andric /* Define functions with static implementation */
268489b1cf2SDimitry Andric #undef ITT_STUB
269489b1cf2SDimitry Andric #undef ITT_STUBV
270fe6060f1SDimitry Andric #define ITT_STUB(api, type, name, args, params, nameindll, group, format)      \
271fe6060f1SDimitry Andric   {ITT_TO_STR(ITT_JOIN(__itt_, nameindll)),                                    \
272fe6060f1SDimitry Andric    (void **)(void *)&ITTNOTIFY_NAME(name),                                     \
273fe6060f1SDimitry Andric    (void *)(size_t)&ITT_VERSIONIZE(ITT_JOIN(_N_(name), _init)),                \
274fe6060f1SDimitry Andric    (void *)(size_t)&ITT_VERSIONIZE(ITT_JOIN(_N_(name), _init)),                \
275fe6060f1SDimitry Andric    (__itt_group_id)(group)},
276489b1cf2SDimitry Andric #define ITT_STUBV ITT_STUB
277489b1cf2SDimitry Andric #define __ITT_INTERNAL_INIT
278489b1cf2SDimitry Andric #include "ittnotify_static.h"
279489b1cf2SDimitry Andric #undef __ITT_INTERNAL_INIT
280489b1cf2SDimitry Andric /* Define functions without static implementation */
281489b1cf2SDimitry Andric #undef ITT_STUB
282489b1cf2SDimitry Andric #undef ITT_STUBV
283fe6060f1SDimitry Andric #define ITT_STUB(api, type, name, args, params, nameindll, group, format)      \
284fe6060f1SDimitry Andric   {ITT_TO_STR(ITT_JOIN(__itt_, nameindll)),                                    \
285fe6060f1SDimitry Andric    (void **)(void *)&ITTNOTIFY_NAME(name),                                     \
286fe6060f1SDimitry Andric    (void *)(size_t)&ITT_VERSIONIZE(ITT_JOIN(_N_(name), _init)), NULL,          \
287fe6060f1SDimitry Andric    (__itt_group_id)(group)},
288489b1cf2SDimitry Andric #define ITT_STUBV ITT_STUB
289489b1cf2SDimitry Andric #include "ittnotify_static.h"
290fe6060f1SDimitry Andric     {NULL, NULL, NULL, NULL, __itt_group_none}};
291489b1cf2SDimitry Andric 
292*349cc55cSDimitry Andric #if ITT_PLATFORM == ITT_PLATFORM_WIN
293*349cc55cSDimitry Andric #if _MSC_VER
294489b1cf2SDimitry Andric #pragma warning(pop)
295*349cc55cSDimitry Andric #endif
296489b1cf2SDimitry Andric #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
297489b1cf2SDimitry Andric 
298489b1cf2SDimitry Andric /* static part descriptor which handles. all notification api attributes. */
299489b1cf2SDimitry Andric __itt_global _N_(_ittapi_global) = {
300489b1cf2SDimitry Andric     ITT_MAGIC, /* identification info */
301fe6060f1SDimitry Andric     ITT_MAJOR,
302fe6060f1SDimitry Andric     ITT_MINOR,
303fe6060f1SDimitry Andric     API_VERSION_BUILD, /* version info */
304489b1cf2SDimitry Andric     0, /* api_initialized */
305489b1cf2SDimitry Andric     0, /* mutex_initialized */
306489b1cf2SDimitry Andric     0, /* atomic_counter */
307489b1cf2SDimitry Andric     MUTEX_INITIALIZER, /* mutex */
308489b1cf2SDimitry Andric     NULL, /* dynamic library handle */
309489b1cf2SDimitry Andric     NULL, /* error_handler */
310489b1cf2SDimitry Andric     NULL, /* dll_path_ptr */
311489b1cf2SDimitry Andric     (__itt_api_info *)&api_list, /* api_list_ptr */
312489b1cf2SDimitry Andric     NULL, /* next __itt_global */
313489b1cf2SDimitry Andric     NULL, /* thread_list */
314489b1cf2SDimitry Andric     NULL, /* domain_list */
315489b1cf2SDimitry Andric     NULL, /* string_list */
316489b1cf2SDimitry Andric     __itt_collection_normal, /* collection state */
317*349cc55cSDimitry Andric     NULL, /* counter_list */
318*349cc55cSDimitry Andric     0, /* ipt_collect_events */
319*349cc55cSDimitry Andric     NULL /* histogram_list */
320489b1cf2SDimitry Andric };
321489b1cf2SDimitry Andric 
322489b1cf2SDimitry Andric typedef void(__itt_api_init_t)(__itt_global *, __itt_group_id);
323489b1cf2SDimitry Andric typedef void(__itt_api_fini_t)(__itt_global *);
324489b1cf2SDimitry Andric 
325*349cc55cSDimitry Andric static __itt_domain dummy_domain;
326489b1cf2SDimitry Andric /* ========================================================================= */
327489b1cf2SDimitry Andric 
328489b1cf2SDimitry Andric #ifdef ITT_NOTIFY_EXT_REPORT
329489b1cf2SDimitry Andric ITT_EXTERN_C void _N_(error_handler)(__itt_error_code, va_list args);
330489b1cf2SDimitry Andric #endif /* ITT_NOTIFY_EXT_REPORT */
331489b1cf2SDimitry Andric 
332*349cc55cSDimitry Andric #if ITT_PLATFORM == ITT_PLATFORM_WIN
333*349cc55cSDimitry Andric #if _MSC_VER
334489b1cf2SDimitry Andric #pragma warning(push)
335*349cc55cSDimitry Andric #pragma warning(                                                               \
336*349cc55cSDimitry Andric     disable : 4055) /* warning C4055: 'type cast' : from data pointer 'void *' \
337*349cc55cSDimitry Andric                        to function pointer 'XXX' */
338*349cc55cSDimitry Andric #endif
339489b1cf2SDimitry Andric #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
340489b1cf2SDimitry Andric 
__itt_report_error(int code,...)341*349cc55cSDimitry Andric static void __itt_report_error(int code, ...) {
342489b1cf2SDimitry Andric   va_list args;
343*349cc55cSDimitry Andric   va_start(args, code);
344fe6060f1SDimitry Andric   if (_N_(_ittapi_global).error_handler != NULL) {
345fe6060f1SDimitry Andric     __itt_error_handler_t *handler =
346fe6060f1SDimitry Andric         (__itt_error_handler_t *)(size_t)_N_(_ittapi_global).error_handler;
347*349cc55cSDimitry Andric     handler((__itt_error_code)code, args);
348489b1cf2SDimitry Andric   }
349489b1cf2SDimitry Andric #ifdef ITT_NOTIFY_EXT_REPORT
350*349cc55cSDimitry Andric   _N_(error_handler)((__itt_error_code)code, args);
351489b1cf2SDimitry Andric #endif /* ITT_NOTIFY_EXT_REPORT */
352489b1cf2SDimitry Andric   va_end(args);
353489b1cf2SDimitry Andric }
354489b1cf2SDimitry Andric 
355*349cc55cSDimitry Andric #if ITT_PLATFORM == ITT_PLATFORM_WIN
356*349cc55cSDimitry Andric #if _MSC_VER
357489b1cf2SDimitry Andric #pragma warning(pop)
358*349cc55cSDimitry Andric #endif
359489b1cf2SDimitry Andric #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
360489b1cf2SDimitry Andric 
361489b1cf2SDimitry Andric #if ITT_PLATFORM == ITT_PLATFORM_WIN
362fe6060f1SDimitry Andric static __itt_domain *ITTAPI
ITT_VERSIONIZE(ITT_JOIN (_N_ (domain_createW),_init))363fe6060f1SDimitry Andric ITT_VERSIONIZE(ITT_JOIN(_N_(domain_createW), _init))(const wchar_t *name) {
364489b1cf2SDimitry Andric   __itt_domain *h_tail = NULL, *h = NULL;
365489b1cf2SDimitry Andric 
366fe6060f1SDimitry Andric   if (name == NULL) {
367489b1cf2SDimitry Andric     return NULL;
368489b1cf2SDimitry Andric   }
369489b1cf2SDimitry Andric 
370489b1cf2SDimitry Andric   ITT_MUTEX_INIT_AND_LOCK(_N_(_ittapi_global));
371fe6060f1SDimitry Andric   if (_N_(_ittapi_global).api_initialized) {
372fe6060f1SDimitry Andric     if (ITTNOTIFY_NAME(domain_createW) &&
373fe6060f1SDimitry Andric         ITTNOTIFY_NAME(domain_createW) !=
374fe6060f1SDimitry Andric             ITT_VERSIONIZE(ITT_JOIN(_N_(domain_createW), _init))) {
375489b1cf2SDimitry Andric       __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
376489b1cf2SDimitry Andric       return ITTNOTIFY_NAME(domain_createW)(name);
377*349cc55cSDimitry Andric     } else {
378*349cc55cSDimitry Andric       __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
379*349cc55cSDimitry Andric       return &dummy_domain;
380489b1cf2SDimitry Andric     }
381489b1cf2SDimitry Andric   }
382fe6060f1SDimitry Andric   for (h_tail = NULL, h = _N_(_ittapi_global).domain_list; h != NULL;
383fe6060f1SDimitry Andric        h_tail = h, h = h->next) {
384fe6060f1SDimitry Andric     if (h->nameW != NULL && !wcscmp(h->nameW, name))
385fe6060f1SDimitry Andric       break;
386489b1cf2SDimitry Andric   }
387fe6060f1SDimitry Andric   if (h == NULL) {
388489b1cf2SDimitry Andric     NEW_DOMAIN_W(&_N_(_ittapi_global), h, h_tail, name);
389489b1cf2SDimitry Andric   }
390fe6060f1SDimitry Andric   if (PTHREAD_SYMBOLS)
391fe6060f1SDimitry Andric     __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
392489b1cf2SDimitry Andric   return h;
393489b1cf2SDimitry Andric }
394489b1cf2SDimitry Andric 
ITT_VERSIONIZE(ITT_JOIN (_N_ (domain_createA),_init))395fe6060f1SDimitry Andric static __itt_domain *ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(domain_createA),
396fe6060f1SDimitry Andric                                                     _init))(const char *name)
397489b1cf2SDimitry Andric #else /* ITT_PLATFORM!=ITT_PLATFORM_WIN */
398fe6060f1SDimitry Andric static __itt_domain *ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(domain_create),
399fe6060f1SDimitry Andric                                                     _init))(const char *name)
400489b1cf2SDimitry Andric #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
401489b1cf2SDimitry Andric {
402489b1cf2SDimitry Andric   __itt_domain *h_tail = NULL, *h = NULL;
403489b1cf2SDimitry Andric 
404fe6060f1SDimitry Andric   if (name == NULL) {
405489b1cf2SDimitry Andric     return NULL;
406489b1cf2SDimitry Andric   }
407489b1cf2SDimitry Andric 
408489b1cf2SDimitry Andric   ITT_MUTEX_INIT_AND_LOCK(_N_(_ittapi_global));
409fe6060f1SDimitry Andric   if (_N_(_ittapi_global).api_initialized) {
410489b1cf2SDimitry Andric #if ITT_PLATFORM == ITT_PLATFORM_WIN
411fe6060f1SDimitry Andric     if (ITTNOTIFY_NAME(domain_createA) &&
412fe6060f1SDimitry Andric         ITTNOTIFY_NAME(domain_createA) !=
413fe6060f1SDimitry Andric             ITT_VERSIONIZE(ITT_JOIN(_N_(domain_createA), _init))) {
414489b1cf2SDimitry Andric       __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
415489b1cf2SDimitry Andric       return ITTNOTIFY_NAME(domain_createA)(name);
416489b1cf2SDimitry Andric     }
417489b1cf2SDimitry Andric #else
418fe6060f1SDimitry Andric     if (ITTNOTIFY_NAME(domain_create) &&
419fe6060f1SDimitry Andric         ITTNOTIFY_NAME(domain_create) !=
420fe6060f1SDimitry Andric             ITT_VERSIONIZE(ITT_JOIN(_N_(domain_create), _init))) {
421fe6060f1SDimitry Andric       if (PTHREAD_SYMBOLS)
422fe6060f1SDimitry Andric         __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
423489b1cf2SDimitry Andric       return ITTNOTIFY_NAME(domain_create)(name);
424489b1cf2SDimitry Andric     }
425489b1cf2SDimitry Andric #endif
426*349cc55cSDimitry Andric     else {
427*349cc55cSDimitry Andric #if ITT_PLATFORM == ITT_PLATFORM_WIN
428*349cc55cSDimitry Andric       __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
429*349cc55cSDimitry Andric #else
430*349cc55cSDimitry Andric       if (PTHREAD_SYMBOLS)
431*349cc55cSDimitry Andric         __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
432*349cc55cSDimitry Andric #endif
433*349cc55cSDimitry Andric       return &dummy_domain;
434*349cc55cSDimitry Andric     }
435489b1cf2SDimitry Andric   }
436fe6060f1SDimitry Andric   for (h_tail = NULL, h = _N_(_ittapi_global).domain_list; h != NULL;
437fe6060f1SDimitry Andric        h_tail = h, h = h->next) {
438fe6060f1SDimitry Andric     if (h->nameA != NULL && !__itt_fstrcmp(h->nameA, name))
439fe6060f1SDimitry Andric       break;
440489b1cf2SDimitry Andric   }
441fe6060f1SDimitry Andric   if (h == NULL) {
442489b1cf2SDimitry Andric     NEW_DOMAIN_A(&_N_(_ittapi_global), h, h_tail, name);
443489b1cf2SDimitry Andric   }
444fe6060f1SDimitry Andric   if (PTHREAD_SYMBOLS)
445fe6060f1SDimitry Andric     __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
446489b1cf2SDimitry Andric   return h;
447489b1cf2SDimitry Andric }
448489b1cf2SDimitry Andric 
ITT_VERSIONIZE(ITT_JOIN (_N_ (module_load_with_sections),_init))449*349cc55cSDimitry Andric static void ITTAPI ITT_VERSIONIZE(ITT_JOIN(
450*349cc55cSDimitry Andric     _N_(module_load_with_sections), _init))(__itt_module_object *module_obj) {
451*349cc55cSDimitry Andric   if (!_N_(_ittapi_global).api_initialized &&
452*349cc55cSDimitry Andric       _N_(_ittapi_global).thread_list == NULL) {
453*349cc55cSDimitry Andric     __itt_init_ittlib_name(NULL, __itt_group_all);
454*349cc55cSDimitry Andric   }
455*349cc55cSDimitry Andric   if (ITTNOTIFY_NAME(module_load_with_sections) &&
456*349cc55cSDimitry Andric       ITTNOTIFY_NAME(module_load_with_sections) !=
457*349cc55cSDimitry Andric           ITT_VERSIONIZE(ITT_JOIN(_N_(module_load_with_sections), _init))) {
458*349cc55cSDimitry Andric     if (module_obj != NULL) {
459*349cc55cSDimitry Andric       module_obj->version = ITT_MODULE_OBJECT_VERSION;
460*349cc55cSDimitry Andric       ITTNOTIFY_NAME(module_load_with_sections)(module_obj);
461*349cc55cSDimitry Andric     }
462*349cc55cSDimitry Andric   }
463*349cc55cSDimitry Andric }
464*349cc55cSDimitry Andric 
ITT_VERSIONIZE(ITT_JOIN (_N_ (module_unload_with_sections),_init))465*349cc55cSDimitry Andric static void ITTAPI ITT_VERSIONIZE(ITT_JOIN(
466*349cc55cSDimitry Andric     _N_(module_unload_with_sections), _init))(__itt_module_object *module_obj) {
467*349cc55cSDimitry Andric   if (!_N_(_ittapi_global).api_initialized &&
468*349cc55cSDimitry Andric       _N_(_ittapi_global).thread_list == NULL) {
469*349cc55cSDimitry Andric     __itt_init_ittlib_name(NULL, __itt_group_all);
470*349cc55cSDimitry Andric   }
471*349cc55cSDimitry Andric   if (ITTNOTIFY_NAME(module_unload_with_sections) &&
472*349cc55cSDimitry Andric       ITTNOTIFY_NAME(module_unload_with_sections) !=
473*349cc55cSDimitry Andric           ITT_VERSIONIZE(ITT_JOIN(_N_(module_unload_with_sections), _init))) {
474*349cc55cSDimitry Andric     if (module_obj != NULL) {
475*349cc55cSDimitry Andric       module_obj->version = ITT_MODULE_OBJECT_VERSION;
476*349cc55cSDimitry Andric       ITTNOTIFY_NAME(module_unload_with_sections)(module_obj);
477*349cc55cSDimitry Andric     }
478*349cc55cSDimitry Andric   }
479*349cc55cSDimitry Andric }
480*349cc55cSDimitry Andric 
481489b1cf2SDimitry Andric #if ITT_PLATFORM == ITT_PLATFORM_WIN
ITT_VERSIONIZE(ITT_JOIN (_N_ (string_handle_createW),_init))482fe6060f1SDimitry Andric static __itt_string_handle *ITTAPI ITT_VERSIONIZE(
483fe6060f1SDimitry Andric     ITT_JOIN(_N_(string_handle_createW), _init))(const wchar_t *name) {
484489b1cf2SDimitry Andric   __itt_string_handle *h_tail = NULL, *h = NULL;
485489b1cf2SDimitry Andric 
486fe6060f1SDimitry Andric   if (name == NULL) {
487489b1cf2SDimitry Andric     return NULL;
488489b1cf2SDimitry Andric   }
489489b1cf2SDimitry Andric 
490489b1cf2SDimitry Andric   ITT_MUTEX_INIT_AND_LOCK(_N_(_ittapi_global));
491fe6060f1SDimitry Andric   if (_N_(_ittapi_global).api_initialized) {
492fe6060f1SDimitry Andric     if (ITTNOTIFY_NAME(string_handle_createW) &&
493fe6060f1SDimitry Andric         ITTNOTIFY_NAME(string_handle_createW) !=
494fe6060f1SDimitry Andric             ITT_VERSIONIZE(ITT_JOIN(_N_(string_handle_createW), _init))) {
495489b1cf2SDimitry Andric       __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
496489b1cf2SDimitry Andric       return ITTNOTIFY_NAME(string_handle_createW)(name);
497*349cc55cSDimitry Andric     } else {
498*349cc55cSDimitry Andric       __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
499*349cc55cSDimitry Andric       return NULL;
500489b1cf2SDimitry Andric     }
501489b1cf2SDimitry Andric   }
502fe6060f1SDimitry Andric   for (h_tail = NULL, h = _N_(_ittapi_global).string_list; h != NULL;
503fe6060f1SDimitry Andric        h_tail = h, h = h->next) {
504fe6060f1SDimitry Andric     if (h->strW != NULL && !wcscmp(h->strW, name))
505fe6060f1SDimitry Andric       break;
506489b1cf2SDimitry Andric   }
507fe6060f1SDimitry Andric   if (h == NULL) {
508489b1cf2SDimitry Andric     NEW_STRING_HANDLE_W(&_N_(_ittapi_global), h, h_tail, name);
509489b1cf2SDimitry Andric   }
510489b1cf2SDimitry Andric   __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
511489b1cf2SDimitry Andric   return h;
512489b1cf2SDimitry Andric }
513489b1cf2SDimitry Andric 
514fe6060f1SDimitry Andric static __itt_string_handle *ITTAPI
ITT_VERSIONIZE(ITT_JOIN (_N_ (string_handle_createA),_init))515fe6060f1SDimitry Andric ITT_VERSIONIZE(ITT_JOIN(_N_(string_handle_createA), _init))(const char *name)
516489b1cf2SDimitry Andric #else /* ITT_PLATFORM!=ITT_PLATFORM_WIN */
517fe6060f1SDimitry Andric static __itt_string_handle *ITTAPI
518fe6060f1SDimitry Andric ITT_VERSIONIZE(ITT_JOIN(_N_(string_handle_create), _init))(const char *name)
519489b1cf2SDimitry Andric #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
520489b1cf2SDimitry Andric {
521489b1cf2SDimitry Andric   __itt_string_handle *h_tail = NULL, *h = NULL;
522489b1cf2SDimitry Andric 
523fe6060f1SDimitry Andric   if (name == NULL) {
524489b1cf2SDimitry Andric     return NULL;
525489b1cf2SDimitry Andric   }
526489b1cf2SDimitry Andric 
527489b1cf2SDimitry Andric   ITT_MUTEX_INIT_AND_LOCK(_N_(_ittapi_global));
528fe6060f1SDimitry Andric   if (_N_(_ittapi_global).api_initialized) {
529489b1cf2SDimitry Andric #if ITT_PLATFORM == ITT_PLATFORM_WIN
530fe6060f1SDimitry Andric     if (ITTNOTIFY_NAME(string_handle_createA) &&
531fe6060f1SDimitry Andric         ITTNOTIFY_NAME(string_handle_createA) !=
532fe6060f1SDimitry Andric             ITT_VERSIONIZE(ITT_JOIN(_N_(string_handle_createA), _init))) {
533489b1cf2SDimitry Andric       __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
534489b1cf2SDimitry Andric       return ITTNOTIFY_NAME(string_handle_createA)(name);
535489b1cf2SDimitry Andric     }
536489b1cf2SDimitry Andric #else
537fe6060f1SDimitry Andric     if (ITTNOTIFY_NAME(string_handle_create) &&
538fe6060f1SDimitry Andric         ITTNOTIFY_NAME(string_handle_create) !=
539fe6060f1SDimitry Andric             ITT_VERSIONIZE(ITT_JOIN(_N_(string_handle_create), _init))) {
540fe6060f1SDimitry Andric       if (PTHREAD_SYMBOLS)
541fe6060f1SDimitry Andric         __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
542489b1cf2SDimitry Andric       return ITTNOTIFY_NAME(string_handle_create)(name);
543489b1cf2SDimitry Andric     }
544489b1cf2SDimitry Andric #endif
545*349cc55cSDimitry Andric     else {
546*349cc55cSDimitry Andric #if ITT_PLATFORM == ITT_PLATFORM_WIN
547*349cc55cSDimitry Andric       __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
548*349cc55cSDimitry Andric #else
549*349cc55cSDimitry Andric       if (PTHREAD_SYMBOLS)
550*349cc55cSDimitry Andric         __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
551*349cc55cSDimitry Andric #endif
552*349cc55cSDimitry Andric       return NULL;
553*349cc55cSDimitry Andric     }
554489b1cf2SDimitry Andric   }
555fe6060f1SDimitry Andric   for (h_tail = NULL, h = _N_(_ittapi_global).string_list; h != NULL;
556fe6060f1SDimitry Andric        h_tail = h, h = h->next) {
557fe6060f1SDimitry Andric     if (h->strA != NULL && !__itt_fstrcmp(h->strA, name))
558fe6060f1SDimitry Andric       break;
559489b1cf2SDimitry Andric   }
560fe6060f1SDimitry Andric   if (h == NULL) {
561489b1cf2SDimitry Andric     NEW_STRING_HANDLE_A(&_N_(_ittapi_global), h, h_tail, name);
562489b1cf2SDimitry Andric   }
563fe6060f1SDimitry Andric   if (PTHREAD_SYMBOLS)
564fe6060f1SDimitry Andric     __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
565489b1cf2SDimitry Andric   return h;
566489b1cf2SDimitry Andric }
567489b1cf2SDimitry Andric 
568489b1cf2SDimitry Andric #if ITT_PLATFORM == ITT_PLATFORM_WIN
ITT_VERSIONIZE(ITT_JOIN (_N_ (counter_createW),_init))569fe6060f1SDimitry Andric static __itt_counter ITTAPI ITT_VERSIONIZE(ITT_JOIN(
570fe6060f1SDimitry Andric     _N_(counter_createW), _init))(const wchar_t *name, const wchar_t *domain) {
571489b1cf2SDimitry Andric   __itt_counter_info_t *h_tail = NULL, *h = NULL;
572489b1cf2SDimitry Andric   __itt_metadata_type type = __itt_metadata_u64;
573489b1cf2SDimitry Andric 
574fe6060f1SDimitry Andric   if (name == NULL) {
575489b1cf2SDimitry Andric     return NULL;
576489b1cf2SDimitry Andric   }
577489b1cf2SDimitry Andric 
578489b1cf2SDimitry Andric   ITT_MUTEX_INIT_AND_LOCK(_N_(_ittapi_global));
579fe6060f1SDimitry Andric   if (_N_(_ittapi_global).api_initialized) {
580fe6060f1SDimitry Andric     if (ITTNOTIFY_NAME(counter_createW) &&
581fe6060f1SDimitry Andric         ITTNOTIFY_NAME(counter_createW) !=
582fe6060f1SDimitry Andric             ITT_VERSIONIZE(ITT_JOIN(_N_(counter_createW), _init))) {
583489b1cf2SDimitry Andric       __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
584489b1cf2SDimitry Andric       return ITTNOTIFY_NAME(counter_createW)(name, domain);
585*349cc55cSDimitry Andric     } else {
586*349cc55cSDimitry Andric       __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
587*349cc55cSDimitry Andric       return NULL;
588489b1cf2SDimitry Andric     }
589489b1cf2SDimitry Andric   }
590fe6060f1SDimitry Andric   for (h_tail = NULL, h = _N_(_ittapi_global).counter_list; h != NULL;
591fe6060f1SDimitry Andric        h_tail = h, h = h->next) {
592*349cc55cSDimitry Andric     if (h->nameW != NULL && h->type == (int)type && !wcscmp(h->nameW, name) &&
593fe6060f1SDimitry Andric         ((h->domainW == NULL && domain == NULL) ||
594fe6060f1SDimitry Andric          (h->domainW != NULL && domain != NULL && !wcscmp(h->domainW, domain))))
595fe6060f1SDimitry Andric       break;
596489b1cf2SDimitry Andric   }
597fe6060f1SDimitry Andric   if (h == NULL) {
598489b1cf2SDimitry Andric     NEW_COUNTER_W(&_N_(_ittapi_global), h, h_tail, name, domain, type);
599489b1cf2SDimitry Andric   }
600489b1cf2SDimitry Andric   __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
601489b1cf2SDimitry Andric   return (__itt_counter)h;
602489b1cf2SDimitry Andric }
603489b1cf2SDimitry Andric 
ITT_VERSIONIZE(ITT_JOIN (_N_ (counter_createA),_init))604fe6060f1SDimitry Andric static __itt_counter ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(counter_createA),
605fe6060f1SDimitry Andric                                                     _init))(const char *name,
606fe6060f1SDimitry Andric                                                             const char *domain)
607489b1cf2SDimitry Andric #else /* ITT_PLATFORM!=ITT_PLATFORM_WIN */
608fe6060f1SDimitry Andric static __itt_counter ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(counter_create),
609fe6060f1SDimitry Andric                                                     _init))(const char *name,
610fe6060f1SDimitry Andric                                                             const char *domain)
611489b1cf2SDimitry Andric #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
612489b1cf2SDimitry Andric {
613489b1cf2SDimitry Andric   __itt_counter_info_t *h_tail = NULL, *h = NULL;
614489b1cf2SDimitry Andric   __itt_metadata_type type = __itt_metadata_u64;
615489b1cf2SDimitry Andric 
616fe6060f1SDimitry Andric   if (name == NULL) {
617489b1cf2SDimitry Andric     return NULL;
618489b1cf2SDimitry Andric   }
619489b1cf2SDimitry Andric 
620489b1cf2SDimitry Andric   ITT_MUTEX_INIT_AND_LOCK(_N_(_ittapi_global));
621fe6060f1SDimitry Andric   if (_N_(_ittapi_global).api_initialized) {
622489b1cf2SDimitry Andric #if ITT_PLATFORM == ITT_PLATFORM_WIN
623fe6060f1SDimitry Andric     if (ITTNOTIFY_NAME(counter_createA) &&
624fe6060f1SDimitry Andric         ITTNOTIFY_NAME(counter_createA) !=
625fe6060f1SDimitry Andric             ITT_VERSIONIZE(ITT_JOIN(_N_(counter_createA), _init))) {
626489b1cf2SDimitry Andric       __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
627489b1cf2SDimitry Andric       return ITTNOTIFY_NAME(counter_createA)(name, domain);
628489b1cf2SDimitry Andric     }
629489b1cf2SDimitry Andric #else
630fe6060f1SDimitry Andric     if (ITTNOTIFY_NAME(counter_create) &&
631fe6060f1SDimitry Andric         ITTNOTIFY_NAME(counter_create) !=
632fe6060f1SDimitry Andric             ITT_VERSIONIZE(ITT_JOIN(_N_(counter_create), _init))) {
633fe6060f1SDimitry Andric       if (PTHREAD_SYMBOLS)
634fe6060f1SDimitry Andric         __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
635489b1cf2SDimitry Andric       return ITTNOTIFY_NAME(counter_create)(name, domain);
636489b1cf2SDimitry Andric     }
637489b1cf2SDimitry Andric #endif
638*349cc55cSDimitry Andric     else {
639*349cc55cSDimitry Andric #if ITT_PLATFORM == ITT_PLATFORM_WIN
640*349cc55cSDimitry Andric       __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
641*349cc55cSDimitry Andric #else
642*349cc55cSDimitry Andric       if (PTHREAD_SYMBOLS)
643*349cc55cSDimitry Andric         __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
644*349cc55cSDimitry Andric #endif
645*349cc55cSDimitry Andric       return NULL;
646*349cc55cSDimitry Andric     }
647489b1cf2SDimitry Andric   }
648fe6060f1SDimitry Andric   for (h_tail = NULL, h = _N_(_ittapi_global).counter_list; h != NULL;
649fe6060f1SDimitry Andric        h_tail = h, h = h->next) {
650*349cc55cSDimitry Andric     if (h->nameA != NULL && h->type == (int)type &&
651*349cc55cSDimitry Andric         !__itt_fstrcmp(h->nameA, name) &&
652fe6060f1SDimitry Andric         ((h->domainA == NULL && domain == NULL) ||
653fe6060f1SDimitry Andric          (h->domainA != NULL && domain != NULL &&
654fe6060f1SDimitry Andric           !__itt_fstrcmp(h->domainA, domain))))
655fe6060f1SDimitry Andric       break;
656489b1cf2SDimitry Andric   }
657fe6060f1SDimitry Andric   if (h == NULL) {
658489b1cf2SDimitry Andric     NEW_COUNTER_A(&_N_(_ittapi_global), h, h_tail, name, domain, type);
659489b1cf2SDimitry Andric   }
660fe6060f1SDimitry Andric   if (PTHREAD_SYMBOLS)
661fe6060f1SDimitry Andric     __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
662489b1cf2SDimitry Andric   return (__itt_counter)h;
663489b1cf2SDimitry Andric }
664489b1cf2SDimitry Andric 
665489b1cf2SDimitry Andric #if ITT_PLATFORM == ITT_PLATFORM_WIN
ITT_VERSIONIZE(ITT_JOIN (_N_ (counter_create_typedW),_init))666fe6060f1SDimitry Andric static __itt_counter ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(counter_create_typedW),
667fe6060f1SDimitry Andric                                                     _init))(
668fe6060f1SDimitry Andric     const wchar_t *name, const wchar_t *domain, __itt_metadata_type type) {
669489b1cf2SDimitry Andric   __itt_counter_info_t *h_tail = NULL, *h = NULL;
670489b1cf2SDimitry Andric 
671fe6060f1SDimitry Andric   if (name == NULL) {
672489b1cf2SDimitry Andric     return NULL;
673489b1cf2SDimitry Andric   }
674489b1cf2SDimitry Andric 
675489b1cf2SDimitry Andric   ITT_MUTEX_INIT_AND_LOCK(_N_(_ittapi_global));
676fe6060f1SDimitry Andric   if (_N_(_ittapi_global).api_initialized) {
677fe6060f1SDimitry Andric     if (ITTNOTIFY_NAME(counter_create_typedW) &&
678fe6060f1SDimitry Andric         ITTNOTIFY_NAME(counter_create_typedW) !=
679fe6060f1SDimitry Andric             ITT_VERSIONIZE(ITT_JOIN(_N_(counter_create_typedW), _init))) {
680489b1cf2SDimitry Andric       __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
681489b1cf2SDimitry Andric       return ITTNOTIFY_NAME(counter_create_typedW)(name, domain, type);
682*349cc55cSDimitry Andric     } else {
683*349cc55cSDimitry Andric       __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
684*349cc55cSDimitry Andric       return NULL;
685489b1cf2SDimitry Andric     }
686489b1cf2SDimitry Andric   }
687fe6060f1SDimitry Andric   for (h_tail = NULL, h = _N_(_ittapi_global).counter_list; h != NULL;
688fe6060f1SDimitry Andric        h_tail = h, h = h->next) {
689*349cc55cSDimitry Andric     if (h->nameW != NULL && h->type == (int)type && !wcscmp(h->nameW, name) &&
690fe6060f1SDimitry Andric         ((h->domainW == NULL && domain == NULL) ||
691fe6060f1SDimitry Andric          (h->domainW != NULL && domain != NULL && !wcscmp(h->domainW, domain))))
692fe6060f1SDimitry Andric       break;
693489b1cf2SDimitry Andric   }
694fe6060f1SDimitry Andric   if (h == NULL) {
695489b1cf2SDimitry Andric     NEW_COUNTER_W(&_N_(_ittapi_global), h, h_tail, name, domain, type);
696489b1cf2SDimitry Andric   }
697489b1cf2SDimitry Andric   __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
698489b1cf2SDimitry Andric   return (__itt_counter)h;
699489b1cf2SDimitry Andric }
700489b1cf2SDimitry Andric 
ITT_VERSIONIZE(ITT_JOIN (_N_ (counter_create_typedA),_init))701fe6060f1SDimitry Andric static __itt_counter ITTAPI ITT_VERSIONIZE(ITT_JOIN(
702fe6060f1SDimitry Andric     _N_(counter_create_typedA), _init))(const char *name, const char *domain,
703fe6060f1SDimitry Andric                                         __itt_metadata_type type)
704489b1cf2SDimitry Andric #else /* ITT_PLATFORM!=ITT_PLATFORM_WIN */
705fe6060f1SDimitry Andric static __itt_counter ITTAPI ITT_VERSIONIZE(ITT_JOIN(
706fe6060f1SDimitry Andric     _N_(counter_create_typed), _init))(const char *name, const char *domain,
707fe6060f1SDimitry Andric                                        __itt_metadata_type type)
708489b1cf2SDimitry Andric #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
709489b1cf2SDimitry Andric {
710489b1cf2SDimitry Andric   __itt_counter_info_t *h_tail = NULL, *h = NULL;
711489b1cf2SDimitry Andric 
712fe6060f1SDimitry Andric   if (name == NULL) {
713489b1cf2SDimitry Andric     return NULL;
714489b1cf2SDimitry Andric   }
715489b1cf2SDimitry Andric 
716489b1cf2SDimitry Andric   ITT_MUTEX_INIT_AND_LOCK(_N_(_ittapi_global));
717fe6060f1SDimitry Andric   if (_N_(_ittapi_global).api_initialized) {
718489b1cf2SDimitry Andric #if ITT_PLATFORM == ITT_PLATFORM_WIN
719fe6060f1SDimitry Andric     if (ITTNOTIFY_NAME(counter_create_typedA) &&
720fe6060f1SDimitry Andric         ITTNOTIFY_NAME(counter_create_typedA) !=
721fe6060f1SDimitry Andric             ITT_VERSIONIZE(ITT_JOIN(_N_(counter_create_typedA), _init))) {
722489b1cf2SDimitry Andric       __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
723489b1cf2SDimitry Andric       return ITTNOTIFY_NAME(counter_create_typedA)(name, domain, type);
724489b1cf2SDimitry Andric     }
725489b1cf2SDimitry Andric #else
726fe6060f1SDimitry Andric     if (ITTNOTIFY_NAME(counter_create_typed) &&
727fe6060f1SDimitry Andric         ITTNOTIFY_NAME(counter_create_typed) !=
728fe6060f1SDimitry Andric             ITT_VERSIONIZE(ITT_JOIN(_N_(counter_create_typed), _init))) {
729fe6060f1SDimitry Andric       if (PTHREAD_SYMBOLS)
730fe6060f1SDimitry Andric         __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
731489b1cf2SDimitry Andric       return ITTNOTIFY_NAME(counter_create_typed)(name, domain, type);
732489b1cf2SDimitry Andric     }
733489b1cf2SDimitry Andric #endif
734*349cc55cSDimitry Andric     else {
735*349cc55cSDimitry Andric #if ITT_PLATFORM == ITT_PLATFORM_WIN
736*349cc55cSDimitry Andric       __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
737*349cc55cSDimitry Andric #else
738*349cc55cSDimitry Andric       if (PTHREAD_SYMBOLS)
739*349cc55cSDimitry Andric         __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
740*349cc55cSDimitry Andric #endif
741*349cc55cSDimitry Andric       return NULL;
742*349cc55cSDimitry Andric     }
743489b1cf2SDimitry Andric   }
744fe6060f1SDimitry Andric   for (h_tail = NULL, h = _N_(_ittapi_global).counter_list; h != NULL;
745fe6060f1SDimitry Andric        h_tail = h, h = h->next) {
746*349cc55cSDimitry Andric     if (h->nameA != NULL && h->type == (int)type &&
747*349cc55cSDimitry Andric         !__itt_fstrcmp(h->nameA, name) &&
748fe6060f1SDimitry Andric         ((h->domainA == NULL && domain == NULL) ||
749fe6060f1SDimitry Andric          (h->domainA != NULL && domain != NULL &&
750fe6060f1SDimitry Andric           !__itt_fstrcmp(h->domainA, domain))))
751fe6060f1SDimitry Andric       break;
752489b1cf2SDimitry Andric   }
753fe6060f1SDimitry Andric   if (h == NULL) {
754489b1cf2SDimitry Andric     NEW_COUNTER_A(&_N_(_ittapi_global), h, h_tail, name, domain, type);
755489b1cf2SDimitry Andric   }
756fe6060f1SDimitry Andric   if (PTHREAD_SYMBOLS)
757fe6060f1SDimitry Andric     __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
758489b1cf2SDimitry Andric   return (__itt_counter)h;
759489b1cf2SDimitry Andric }
760489b1cf2SDimitry Andric 
761*349cc55cSDimitry Andric #if ITT_PLATFORM == ITT_PLATFORM_WIN
ITT_VERSIONIZE(ITT_JOIN (_N_ (histogram_createW),_init))762*349cc55cSDimitry Andric static __itt_histogram *ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(histogram_createW),
763*349cc55cSDimitry Andric                                                        _init))(
764*349cc55cSDimitry Andric     const __itt_domain *domain, const wchar_t *name, __itt_metadata_type x_type,
765*349cc55cSDimitry Andric     __itt_metadata_type y_type) {
766*349cc55cSDimitry Andric   __itt_histogram *h_tail = NULL, *h = NULL;
767*349cc55cSDimitry Andric 
768*349cc55cSDimitry Andric   if (domain == NULL || name == NULL) {
769*349cc55cSDimitry Andric     return NULL;
770*349cc55cSDimitry Andric   }
771*349cc55cSDimitry Andric 
772*349cc55cSDimitry Andric   ITT_MUTEX_INIT_AND_LOCK(_N_(_ittapi_global));
773*349cc55cSDimitry Andric   if (_N_(_ittapi_global).api_initialized) {
774*349cc55cSDimitry Andric     if (ITTNOTIFY_NAME(histogram_createW) &&
775*349cc55cSDimitry Andric         ITTNOTIFY_NAME(histogram_createW) !=
776*349cc55cSDimitry Andric             ITT_VERSIONIZE(ITT_JOIN(_N_(histogram_createW), _init))) {
777*349cc55cSDimitry Andric       __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
778*349cc55cSDimitry Andric       return ITTNOTIFY_NAME(histogram_createW)(domain, name, x_type, y_type);
779*349cc55cSDimitry Andric     } else {
780*349cc55cSDimitry Andric       __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
781*349cc55cSDimitry Andric       return NULL;
782*349cc55cSDimitry Andric     }
783*349cc55cSDimitry Andric   }
784*349cc55cSDimitry Andric   for (h_tail = NULL, h = _N_(_ittapi_global).histogram_list; h != NULL;
785*349cc55cSDimitry Andric        h_tail = h, h = h->next) {
786*349cc55cSDimitry Andric     if (h->domain == NULL)
787*349cc55cSDimitry Andric       continue;
788*349cc55cSDimitry Andric     else if (h->domain != domain && h->nameW != NULL && !wcscmp(h->nameW, name))
789*349cc55cSDimitry Andric       break;
790*349cc55cSDimitry Andric   }
791*349cc55cSDimitry Andric   if (h == NULL) {
792*349cc55cSDimitry Andric     NEW_HISTOGRAM_W(&_N_(_ittapi_global), h, h_tail, domain, name, x_type,
793*349cc55cSDimitry Andric                     y_type);
794*349cc55cSDimitry Andric   }
795*349cc55cSDimitry Andric   __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
796*349cc55cSDimitry Andric   return (__itt_histogram *)h;
797*349cc55cSDimitry Andric }
798*349cc55cSDimitry Andric 
ITT_VERSIONIZE(ITT_JOIN (_N_ (histogram_createA),_init))799*349cc55cSDimitry Andric static __itt_histogram *ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(histogram_createA),
800*349cc55cSDimitry Andric                                                        _init))(
801*349cc55cSDimitry Andric     const __itt_domain *domain, const char *name, __itt_metadata_type x_type,
802*349cc55cSDimitry Andric     __itt_metadata_type y_type)
803*349cc55cSDimitry Andric #else /* ITT_PLATFORM!=ITT_PLATFORM_WIN */
804*349cc55cSDimitry Andric static __itt_histogram *ITTAPI ITT_VERSIONIZE(ITT_JOIN(
805*349cc55cSDimitry Andric     _N_(histogram_create), _init))(const __itt_domain *domain, const char *name,
806*349cc55cSDimitry Andric                                    __itt_metadata_type x_type,
807*349cc55cSDimitry Andric                                    __itt_metadata_type y_type)
808*349cc55cSDimitry Andric #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
809*349cc55cSDimitry Andric {
810*349cc55cSDimitry Andric   __itt_histogram *h_tail = NULL, *h = NULL;
811*349cc55cSDimitry Andric 
812*349cc55cSDimitry Andric   if (domain == NULL || name == NULL) {
813*349cc55cSDimitry Andric     return NULL;
814*349cc55cSDimitry Andric   }
815*349cc55cSDimitry Andric 
816*349cc55cSDimitry Andric   ITT_MUTEX_INIT_AND_LOCK(_N_(_ittapi_global));
817*349cc55cSDimitry Andric   if (_N_(_ittapi_global).api_initialized) {
818*349cc55cSDimitry Andric #if ITT_PLATFORM == ITT_PLATFORM_WIN
819*349cc55cSDimitry Andric     if (ITTNOTIFY_NAME(histogram_createA) &&
820*349cc55cSDimitry Andric         ITTNOTIFY_NAME(histogram_createA) !=
821*349cc55cSDimitry Andric             ITT_VERSIONIZE(ITT_JOIN(_N_(histogram_createA), _init))) {
822*349cc55cSDimitry Andric       __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
823*349cc55cSDimitry Andric       return ITTNOTIFY_NAME(histogram_createA)(domain, name, x_type, y_type);
824*349cc55cSDimitry Andric     }
825*349cc55cSDimitry Andric #else
826*349cc55cSDimitry Andric     if (ITTNOTIFY_NAME(histogram_create) &&
827*349cc55cSDimitry Andric         ITTNOTIFY_NAME(histogram_create) !=
828*349cc55cSDimitry Andric             ITT_VERSIONIZE(ITT_JOIN(_N_(histogram_create), _init))) {
829*349cc55cSDimitry Andric       if (PTHREAD_SYMBOLS)
830*349cc55cSDimitry Andric         __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
831*349cc55cSDimitry Andric       return ITTNOTIFY_NAME(histogram_create)(domain, name, x_type, y_type);
832*349cc55cSDimitry Andric     }
833*349cc55cSDimitry Andric #endif
834*349cc55cSDimitry Andric     else {
835*349cc55cSDimitry Andric #if ITT_PLATFORM == ITT_PLATFORM_WIN
836*349cc55cSDimitry Andric       __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
837*349cc55cSDimitry Andric #else
838*349cc55cSDimitry Andric       if (PTHREAD_SYMBOLS)
839*349cc55cSDimitry Andric         __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
840*349cc55cSDimitry Andric #endif
841*349cc55cSDimitry Andric       return NULL;
842*349cc55cSDimitry Andric     }
843*349cc55cSDimitry Andric   }
844*349cc55cSDimitry Andric   for (h_tail = NULL, h = _N_(_ittapi_global).histogram_list; h != NULL;
845*349cc55cSDimitry Andric        h_tail = h, h = h->next) {
846*349cc55cSDimitry Andric     if (h->domain == NULL)
847*349cc55cSDimitry Andric       continue;
848*349cc55cSDimitry Andric     else if (h->domain != domain && h->nameA != NULL &&
849*349cc55cSDimitry Andric              !__itt_fstrcmp(h->nameA, name))
850*349cc55cSDimitry Andric       break;
851*349cc55cSDimitry Andric   }
852*349cc55cSDimitry Andric   if (h == NULL) {
853*349cc55cSDimitry Andric     NEW_HISTOGRAM_A(&_N_(_ittapi_global), h, h_tail, domain, name, x_type,
854*349cc55cSDimitry Andric                     y_type);
855*349cc55cSDimitry Andric   }
856*349cc55cSDimitry Andric   if (PTHREAD_SYMBOLS)
857*349cc55cSDimitry Andric     __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
858*349cc55cSDimitry Andric   return (__itt_histogram *)h;
859*349cc55cSDimitry Andric }
860*349cc55cSDimitry Andric 
861489b1cf2SDimitry Andric /* -------------------------------------------------------------------------- */
862489b1cf2SDimitry Andric 
ITT_VERSIONIZE(ITT_JOIN (_N_ (pause),_init))863fe6060f1SDimitry Andric static void ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(pause), _init))(void) {
864fe6060f1SDimitry Andric   if (!_N_(_ittapi_global).api_initialized &&
865fe6060f1SDimitry Andric       _N_(_ittapi_global).thread_list == NULL) {
866489b1cf2SDimitry Andric     __itt_init_ittlib_name(NULL, __itt_group_all);
867489b1cf2SDimitry Andric   }
868fe6060f1SDimitry Andric   if (ITTNOTIFY_NAME(pause) &&
869fe6060f1SDimitry Andric       ITTNOTIFY_NAME(pause) != ITT_VERSIONIZE(ITT_JOIN(_N_(pause), _init))) {
870489b1cf2SDimitry Andric     ITTNOTIFY_NAME(pause)();
871fe6060f1SDimitry Andric   } else {
872489b1cf2SDimitry Andric     _N_(_ittapi_global).state = __itt_collection_paused;
873489b1cf2SDimitry Andric   }
874489b1cf2SDimitry Andric }
875489b1cf2SDimitry Andric 
ITT_VERSIONIZE(ITT_JOIN (_N_ (resume),_init))876fe6060f1SDimitry Andric static void ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(resume), _init))(void) {
877fe6060f1SDimitry Andric   if (!_N_(_ittapi_global).api_initialized &&
878fe6060f1SDimitry Andric       _N_(_ittapi_global).thread_list == NULL) {
879489b1cf2SDimitry Andric     __itt_init_ittlib_name(NULL, __itt_group_all);
880489b1cf2SDimitry Andric   }
881fe6060f1SDimitry Andric   if (ITTNOTIFY_NAME(resume) &&
882fe6060f1SDimitry Andric       ITTNOTIFY_NAME(resume) != ITT_VERSIONIZE(ITT_JOIN(_N_(resume), _init))) {
883489b1cf2SDimitry Andric     ITTNOTIFY_NAME(resume)();
884fe6060f1SDimitry Andric   } else {
885489b1cf2SDimitry Andric     _N_(_ittapi_global).state = __itt_collection_normal;
886489b1cf2SDimitry Andric   }
887489b1cf2SDimitry Andric }
888489b1cf2SDimitry Andric 
889489b1cf2SDimitry Andric #if ITT_PLATFORM == ITT_PLATFORM_WIN
ITT_VERSIONIZE(ITT_JOIN (_N_ (thread_set_nameW),_init))890fe6060f1SDimitry Andric static void ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(thread_set_nameW),
891fe6060f1SDimitry Andric                                            _init))(const wchar_t *name) {
892fe6060f1SDimitry Andric   if (!_N_(_ittapi_global).api_initialized &&
893fe6060f1SDimitry Andric       _N_(_ittapi_global).thread_list == NULL) {
894489b1cf2SDimitry Andric     __itt_init_ittlib_name(NULL, __itt_group_all);
895489b1cf2SDimitry Andric   }
896fe6060f1SDimitry Andric   if (ITTNOTIFY_NAME(thread_set_nameW) &&
897fe6060f1SDimitry Andric       ITTNOTIFY_NAME(thread_set_nameW) !=
898fe6060f1SDimitry Andric           ITT_VERSIONIZE(ITT_JOIN(_N_(thread_set_nameW), _init))) {
899489b1cf2SDimitry Andric     ITTNOTIFY_NAME(thread_set_nameW)(name);
900489b1cf2SDimitry Andric   }
901489b1cf2SDimitry Andric }
902489b1cf2SDimitry Andric 
ITT_VERSIONIZE(ITT_JOIN (_N_ (thr_name_setW),_init))903fe6060f1SDimitry Andric static int ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(thr_name_setW),
904fe6060f1SDimitry Andric                                           _init))(const wchar_t *name,
905fe6060f1SDimitry Andric                                                   int namelen) {
906489b1cf2SDimitry Andric   (void)namelen;
907489b1cf2SDimitry Andric   ITT_VERSIONIZE(ITT_JOIN(_N_(thread_set_nameW), _init))(name);
908489b1cf2SDimitry Andric   return 0;
909489b1cf2SDimitry Andric }
910489b1cf2SDimitry Andric 
ITT_VERSIONIZE(ITT_JOIN (_N_ (thread_set_nameA),_init))911fe6060f1SDimitry Andric static void ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(thread_set_nameA),
912fe6060f1SDimitry Andric                                            _init))(const char *name)
913489b1cf2SDimitry Andric #else /* ITT_PLATFORM==ITT_PLATFORM_WIN */
914fe6060f1SDimitry Andric static void ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(thread_set_name),
915fe6060f1SDimitry Andric                                            _init))(const char *name)
916489b1cf2SDimitry Andric #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
917489b1cf2SDimitry Andric {
918fe6060f1SDimitry Andric   if (!_N_(_ittapi_global).api_initialized &&
919fe6060f1SDimitry Andric       _N_(_ittapi_global).thread_list == NULL) {
920489b1cf2SDimitry Andric     __itt_init_ittlib_name(NULL, __itt_group_all);
921489b1cf2SDimitry Andric   }
922489b1cf2SDimitry Andric #if ITT_PLATFORM == ITT_PLATFORM_WIN
923fe6060f1SDimitry Andric   if (ITTNOTIFY_NAME(thread_set_nameA) &&
924fe6060f1SDimitry Andric       ITTNOTIFY_NAME(thread_set_nameA) !=
925fe6060f1SDimitry Andric           ITT_VERSIONIZE(ITT_JOIN(_N_(thread_set_nameA), _init))) {
926489b1cf2SDimitry Andric     ITTNOTIFY_NAME(thread_set_nameA)(name);
927489b1cf2SDimitry Andric   }
928489b1cf2SDimitry Andric #else /* ITT_PLATFORM==ITT_PLATFORM_WIN */
929fe6060f1SDimitry Andric   if (ITTNOTIFY_NAME(thread_set_name) &&
930fe6060f1SDimitry Andric       ITTNOTIFY_NAME(thread_set_name) !=
931fe6060f1SDimitry Andric           ITT_VERSIONIZE(ITT_JOIN(_N_(thread_set_name), _init))) {
932489b1cf2SDimitry Andric     ITTNOTIFY_NAME(thread_set_name)(name);
933489b1cf2SDimitry Andric   }
934489b1cf2SDimitry Andric #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
935489b1cf2SDimitry Andric }
936489b1cf2SDimitry Andric 
937489b1cf2SDimitry Andric #if ITT_PLATFORM == ITT_PLATFORM_WIN
ITT_VERSIONIZE(ITT_JOIN (_N_ (thr_name_setA),_init))938fe6060f1SDimitry Andric static int ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(thr_name_setA),
939fe6060f1SDimitry Andric                                           _init))(const char *name,
940fe6060f1SDimitry Andric                                                   int namelen) {
941489b1cf2SDimitry Andric   (void)namelen;
942489b1cf2SDimitry Andric   ITT_VERSIONIZE(ITT_JOIN(_N_(thread_set_nameA), _init))(name);
943489b1cf2SDimitry Andric   return 0;
944489b1cf2SDimitry Andric }
945489b1cf2SDimitry Andric #else /* ITT_PLATFORM==ITT_PLATFORM_WIN */
ITT_VERSIONIZE(ITT_JOIN (_N_ (thr_name_set),_init))946fe6060f1SDimitry Andric static int ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(thr_name_set),
947fe6060f1SDimitry Andric                                           _init))(const char *name,
948fe6060f1SDimitry Andric                                                   int namelen) {
949489b1cf2SDimitry Andric   (void)namelen;
950489b1cf2SDimitry Andric   ITT_VERSIONIZE(ITT_JOIN(_N_(thread_set_name), _init))(name);
951489b1cf2SDimitry Andric   return 0;
952489b1cf2SDimitry Andric }
953489b1cf2SDimitry Andric #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
954489b1cf2SDimitry Andric 
ITT_VERSIONIZE(ITT_JOIN (_N_ (thread_ignore),_init))955fe6060f1SDimitry Andric static void ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(thread_ignore), _init))(void) {
956fe6060f1SDimitry Andric   if (!_N_(_ittapi_global).api_initialized &&
957fe6060f1SDimitry Andric       _N_(_ittapi_global).thread_list == NULL) {
958489b1cf2SDimitry Andric     __itt_init_ittlib_name(NULL, __itt_group_all);
959489b1cf2SDimitry Andric   }
960fe6060f1SDimitry Andric   if (ITTNOTIFY_NAME(thread_ignore) &&
961fe6060f1SDimitry Andric       ITTNOTIFY_NAME(thread_ignore) !=
962fe6060f1SDimitry Andric           ITT_VERSIONIZE(ITT_JOIN(_N_(thread_ignore), _init))) {
963489b1cf2SDimitry Andric     ITTNOTIFY_NAME(thread_ignore)();
964489b1cf2SDimitry Andric   }
965489b1cf2SDimitry Andric }
966489b1cf2SDimitry Andric 
ITT_VERSIONIZE(ITT_JOIN (_N_ (thr_ignore),_init))967fe6060f1SDimitry Andric static void ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(thr_ignore), _init))(void) {
968489b1cf2SDimitry Andric   ITT_VERSIONIZE(ITT_JOIN(_N_(thread_ignore), _init))();
969489b1cf2SDimitry Andric }
970489b1cf2SDimitry Andric 
ITT_VERSIONIZE(ITT_JOIN (_N_ (enable_attach),_init))971fe6060f1SDimitry Andric static void ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(enable_attach), _init))(void) {
972489b1cf2SDimitry Andric #ifdef __ANDROID__
973489b1cf2SDimitry Andric   /*
974489b1cf2SDimitry Andric    * if LIB_VAR_NAME env variable were set before then stay previous value
975489b1cf2SDimitry Andric    * else set default path
976489b1cf2SDimitry Andric    */
977489b1cf2SDimitry Andric   setenv(ITT_TO_STR(LIB_VAR_NAME), ANDROID_ITTNOTIFY_DEFAULT_PATH, 0);
978489b1cf2SDimitry Andric #endif
979489b1cf2SDimitry Andric }
980489b1cf2SDimitry Andric 
981489b1cf2SDimitry Andric /* -------------------------------------------------------------------------- */
982489b1cf2SDimitry Andric 
__itt_fsplit(const char * s,const char * sep,const char ** out,int * len)983fe6060f1SDimitry Andric static const char *__itt_fsplit(const char *s, const char *sep,
984fe6060f1SDimitry Andric                                 const char **out, int *len) {
985489b1cf2SDimitry Andric   int i;
986489b1cf2SDimitry Andric   int j;
987489b1cf2SDimitry Andric 
988489b1cf2SDimitry Andric   if (!s || !sep || !out || !len)
989489b1cf2SDimitry Andric     return NULL;
990489b1cf2SDimitry Andric 
991fe6060f1SDimitry Andric   for (i = 0; s[i]; i++) {
992489b1cf2SDimitry Andric     int b = 0;
993489b1cf2SDimitry Andric     for (j = 0; sep[j]; j++)
994fe6060f1SDimitry Andric       if (s[i] == sep[j]) {
995489b1cf2SDimitry Andric         b = 1;
996489b1cf2SDimitry Andric         break;
997489b1cf2SDimitry Andric       }
998489b1cf2SDimitry Andric     if (!b)
999489b1cf2SDimitry Andric       break;
1000489b1cf2SDimitry Andric   }
1001489b1cf2SDimitry Andric 
1002489b1cf2SDimitry Andric   if (!s[i])
1003489b1cf2SDimitry Andric     return NULL;
1004489b1cf2SDimitry Andric 
1005489b1cf2SDimitry Andric   *len = 0;
1006489b1cf2SDimitry Andric   *out = &s[i];
1007489b1cf2SDimitry Andric 
1008fe6060f1SDimitry Andric   for (; s[i]; i++, (*len)++) {
1009489b1cf2SDimitry Andric     int b = 0;
1010489b1cf2SDimitry Andric     for (j = 0; sep[j]; j++)
1011fe6060f1SDimitry Andric       if (s[i] == sep[j]) {
1012489b1cf2SDimitry Andric         b = 1;
1013489b1cf2SDimitry Andric         break;
1014489b1cf2SDimitry Andric       }
1015489b1cf2SDimitry Andric     if (b)
1016489b1cf2SDimitry Andric       break;
1017489b1cf2SDimitry Andric   }
1018489b1cf2SDimitry Andric 
1019fe6060f1SDimitry Andric   for (; s[i]; i++) {
1020489b1cf2SDimitry Andric     int b = 0;
1021489b1cf2SDimitry Andric     for (j = 0; sep[j]; j++)
1022fe6060f1SDimitry Andric       if (s[i] == sep[j]) {
1023489b1cf2SDimitry Andric         b = 1;
1024489b1cf2SDimitry Andric         break;
1025489b1cf2SDimitry Andric       }
1026489b1cf2SDimitry Andric     if (!b)
1027489b1cf2SDimitry Andric       break;
1028489b1cf2SDimitry Andric   }
1029489b1cf2SDimitry Andric 
1030489b1cf2SDimitry Andric   return &s[i];
1031489b1cf2SDimitry Andric }
1032489b1cf2SDimitry Andric 
1033489b1cf2SDimitry Andric /* This function return value of env variable that placed into static buffer.
1034489b1cf2SDimitry Andric  * !!! The same static buffer is used for subsequent calls. !!!
10355ffd83dbSDimitry Andric  * This was done to avoid dynamic allocation for few calls.
1036489b1cf2SDimitry Andric  * Actually we need this function only four times.
1037489b1cf2SDimitry Andric  */
__itt_get_env_var(const char * name)1038fe6060f1SDimitry Andric static const char *__itt_get_env_var(const char *name) {
1039489b1cf2SDimitry Andric #define MAX_ENV_VALUE_SIZE 4086
1040489b1cf2SDimitry Andric   static char env_buff[MAX_ENV_VALUE_SIZE];
1041489b1cf2SDimitry Andric   static char *env_value = (char *)env_buff;
1042489b1cf2SDimitry Andric 
1043fe6060f1SDimitry Andric   if (name != NULL) {
1044489b1cf2SDimitry Andric #if ITT_PLATFORM == ITT_PLATFORM_WIN
1045489b1cf2SDimitry Andric     size_t max_len = MAX_ENV_VALUE_SIZE - (size_t)(env_value - env_buff);
1046489b1cf2SDimitry Andric     DWORD rc = GetEnvironmentVariableA(name, env_value, (DWORD)max_len);
1047489b1cf2SDimitry Andric     if (rc >= max_len)
1048fe6060f1SDimitry Andric       __itt_report_error(__itt_error_env_too_long, name, (size_t)rc - 1,
1049fe6060f1SDimitry Andric                          (size_t)(max_len - 1));
1050fe6060f1SDimitry Andric     else if (rc > 0) {
1051489b1cf2SDimitry Andric       const char *ret = (const char *)env_value;
1052489b1cf2SDimitry Andric       env_value += rc + 1;
1053489b1cf2SDimitry Andric       return ret;
1054fe6060f1SDimitry Andric     } else {
1055480093f4SDimitry Andric       /* If environment variable is empty, GetEnvironmentVariables()
1056489b1cf2SDimitry Andric        * returns zero (number of characters (not including terminating null),
1057489b1cf2SDimitry Andric        * and GetLastError() returns ERROR_SUCCESS. */
1058489b1cf2SDimitry Andric       DWORD err = GetLastError();
1059489b1cf2SDimitry Andric       if (err == ERROR_SUCCESS)
1060489b1cf2SDimitry Andric         return env_value;
1061489b1cf2SDimitry Andric 
1062489b1cf2SDimitry Andric       if (err != ERROR_ENVVAR_NOT_FOUND)
1063489b1cf2SDimitry Andric         __itt_report_error(__itt_error_cant_read_env, name, (int)err);
1064489b1cf2SDimitry Andric     }
1065489b1cf2SDimitry Andric #else /* ITT_PLATFORM!=ITT_PLATFORM_WIN */
1066489b1cf2SDimitry Andric     char *env = getenv(name);
1067fe6060f1SDimitry Andric     if (env != NULL) {
1068489b1cf2SDimitry Andric       size_t len = __itt_fstrnlen(env, MAX_ENV_VALUE_SIZE);
1069489b1cf2SDimitry Andric       size_t max_len = MAX_ENV_VALUE_SIZE - (size_t)(env_value - env_buff);
1070fe6060f1SDimitry Andric       if (len < max_len) {
1071489b1cf2SDimitry Andric         const char *ret = (const char *)env_value;
1072489b1cf2SDimitry Andric         __itt_fstrcpyn(env_value, max_len, env, len + 1);
1073489b1cf2SDimitry Andric         env_value += len + 1;
1074489b1cf2SDimitry Andric         return ret;
1075489b1cf2SDimitry Andric       } else
1076fe6060f1SDimitry Andric         __itt_report_error(__itt_error_env_too_long, name, (size_t)len,
1077fe6060f1SDimitry Andric                            (size_t)(max_len - 1));
1078489b1cf2SDimitry Andric     }
1079489b1cf2SDimitry Andric #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
1080489b1cf2SDimitry Andric   }
1081489b1cf2SDimitry Andric   return NULL;
1082489b1cf2SDimitry Andric }
1083489b1cf2SDimitry Andric 
__itt_get_lib_name(void)1084fe6060f1SDimitry Andric static const char *__itt_get_lib_name(void) {
1085489b1cf2SDimitry Andric   const char *lib_name = __itt_get_env_var(ITT_TO_STR(LIB_VAR_NAME));
1086489b1cf2SDimitry Andric 
1087489b1cf2SDimitry Andric #ifdef __ANDROID__
1088fe6060f1SDimitry Andric   if (lib_name == NULL) {
1089489b1cf2SDimitry Andric 
1090489b1cf2SDimitry Andric #if ITT_ARCH == ITT_ARCH_IA32 || ITT_ARCH == ITT_ARCH_ARM
1091489b1cf2SDimitry Andric     const char *const marker_filename = "com.intel.itt.collector_lib_32";
1092489b1cf2SDimitry Andric #else
1093489b1cf2SDimitry Andric     const char *const marker_filename = "com.intel.itt.collector_lib_64";
1094489b1cf2SDimitry Andric #endif
1095489b1cf2SDimitry Andric 
1096489b1cf2SDimitry Andric     char system_wide_marker_filename[PATH_MAX] = {0};
1097489b1cf2SDimitry Andric     int itt_marker_file_fd = -1;
1098489b1cf2SDimitry Andric     ssize_t res = 0;
1099489b1cf2SDimitry Andric 
1100fe6060f1SDimitry Andric     res = snprintf(system_wide_marker_filename, PATH_MAX - 1, "%s%s",
1101fe6060f1SDimitry Andric                    "/data/local/tmp/", marker_filename);
1102fe6060f1SDimitry Andric     if (res < 0) {
1103489b1cf2SDimitry Andric       ITT_ANDROID_LOGE("Unable to concatenate marker file string.");
1104489b1cf2SDimitry Andric       return lib_name;
1105489b1cf2SDimitry Andric     }
1106489b1cf2SDimitry Andric     itt_marker_file_fd = open(system_wide_marker_filename, O_RDONLY);
1107489b1cf2SDimitry Andric 
1108fe6060f1SDimitry Andric     if (itt_marker_file_fd == -1) {
1109489b1cf2SDimitry Andric       const pid_t my_pid = getpid();
1110489b1cf2SDimitry Andric       char cmdline_path[PATH_MAX] = {0};
1111489b1cf2SDimitry Andric       char package_name[PATH_MAX] = {0};
1112489b1cf2SDimitry Andric       char app_sandbox_file[PATH_MAX] = {0};
1113489b1cf2SDimitry Andric       int cmdline_fd = 0;
1114489b1cf2SDimitry Andric 
1115489b1cf2SDimitry Andric       ITT_ANDROID_LOGI("Unable to open system-wide marker file.");
1116489b1cf2SDimitry Andric       res = snprintf(cmdline_path, PATH_MAX - 1, "/proc/%d/cmdline", my_pid);
1117fe6060f1SDimitry Andric       if (res < 0) {
1118489b1cf2SDimitry Andric         ITT_ANDROID_LOGE("Unable to get cmdline path string.");
1119489b1cf2SDimitry Andric         return lib_name;
1120489b1cf2SDimitry Andric       }
1121489b1cf2SDimitry Andric 
1122489b1cf2SDimitry Andric       ITT_ANDROID_LOGI("CMD file: %s\n", cmdline_path);
1123489b1cf2SDimitry Andric       cmdline_fd = open(cmdline_path, O_RDONLY);
1124fe6060f1SDimitry Andric       if (cmdline_fd == -1) {
1125489b1cf2SDimitry Andric         ITT_ANDROID_LOGE("Unable to open %s file!", cmdline_path);
1126489b1cf2SDimitry Andric         return lib_name;
1127489b1cf2SDimitry Andric       }
1128489b1cf2SDimitry Andric       res = read(cmdline_fd, package_name, PATH_MAX - 1);
1129fe6060f1SDimitry Andric       if (res == -1) {
1130489b1cf2SDimitry Andric         ITT_ANDROID_LOGE("Unable to read %s file!", cmdline_path);
1131489b1cf2SDimitry Andric         res = close(cmdline_fd);
1132fe6060f1SDimitry Andric         if (res == -1) {
1133489b1cf2SDimitry Andric           ITT_ANDROID_LOGE("Unable to close %s file!", cmdline_path);
1134489b1cf2SDimitry Andric         }
1135489b1cf2SDimitry Andric         return lib_name;
1136489b1cf2SDimitry Andric       }
1137489b1cf2SDimitry Andric       res = close(cmdline_fd);
1138fe6060f1SDimitry Andric       if (res == -1) {
1139489b1cf2SDimitry Andric         ITT_ANDROID_LOGE("Unable to close %s file!", cmdline_path);
1140489b1cf2SDimitry Andric         return lib_name;
1141489b1cf2SDimitry Andric       }
1142489b1cf2SDimitry Andric       ITT_ANDROID_LOGI("Package name: %s\n", package_name);
1143fe6060f1SDimitry Andric       res = snprintf(app_sandbox_file, PATH_MAX - 1, "/data/data/%s/%s",
1144fe6060f1SDimitry Andric                      package_name, marker_filename);
1145fe6060f1SDimitry Andric       if (res < 0) {
1146489b1cf2SDimitry Andric         ITT_ANDROID_LOGE("Unable to concatenate marker file string.");
1147489b1cf2SDimitry Andric         return lib_name;
1148489b1cf2SDimitry Andric       }
1149489b1cf2SDimitry Andric 
1150489b1cf2SDimitry Andric       ITT_ANDROID_LOGI("Lib marker file name: %s\n", app_sandbox_file);
1151489b1cf2SDimitry Andric       itt_marker_file_fd = open(app_sandbox_file, O_RDONLY);
1152fe6060f1SDimitry Andric       if (itt_marker_file_fd == -1) {
1153489b1cf2SDimitry Andric         ITT_ANDROID_LOGE("Unable to open app marker file!");
1154489b1cf2SDimitry Andric         return lib_name;
1155489b1cf2SDimitry Andric       }
1156489b1cf2SDimitry Andric     }
1157489b1cf2SDimitry Andric 
1158489b1cf2SDimitry Andric     {
1159489b1cf2SDimitry Andric       char itt_lib_name[PATH_MAX] = {0};
1160489b1cf2SDimitry Andric 
1161489b1cf2SDimitry Andric       res = read(itt_marker_file_fd, itt_lib_name, PATH_MAX - 1);
1162fe6060f1SDimitry Andric       if (res == -1) {
1163489b1cf2SDimitry Andric         ITT_ANDROID_LOGE("Unable to read %s file!", itt_marker_file_fd);
1164489b1cf2SDimitry Andric         res = close(itt_marker_file_fd);
1165fe6060f1SDimitry Andric         if (res == -1) {
1166489b1cf2SDimitry Andric           ITT_ANDROID_LOGE("Unable to close %s file!", itt_marker_file_fd);
1167489b1cf2SDimitry Andric         }
1168489b1cf2SDimitry Andric         return lib_name;
1169489b1cf2SDimitry Andric       }
1170489b1cf2SDimitry Andric       ITT_ANDROID_LOGI("ITT Lib path: %s", itt_lib_name);
1171489b1cf2SDimitry Andric       res = close(itt_marker_file_fd);
1172fe6060f1SDimitry Andric       if (res == -1) {
1173489b1cf2SDimitry Andric         ITT_ANDROID_LOGE("Unable to close %s file!", itt_marker_file_fd);
1174489b1cf2SDimitry Andric         return lib_name;
1175489b1cf2SDimitry Andric       }
1176fe6060f1SDimitry Andric       ITT_ANDROID_LOGI("Set env %s to %s", ITT_TO_STR(LIB_VAR_NAME),
1177fe6060f1SDimitry Andric                        itt_lib_name);
1178489b1cf2SDimitry Andric       res = setenv(ITT_TO_STR(LIB_VAR_NAME), itt_lib_name, 0);
1179fe6060f1SDimitry Andric       if (res == -1) {
1180489b1cf2SDimitry Andric         ITT_ANDROID_LOGE("Unable to set env var!");
1181489b1cf2SDimitry Andric         return lib_name;
1182489b1cf2SDimitry Andric       }
1183489b1cf2SDimitry Andric       lib_name = __itt_get_env_var(ITT_TO_STR(LIB_VAR_NAME));
1184489b1cf2SDimitry Andric       ITT_ANDROID_LOGI("ITT Lib path from env: %s", lib_name);
1185489b1cf2SDimitry Andric     }
1186489b1cf2SDimitry Andric   }
1187489b1cf2SDimitry Andric #endif
1188489b1cf2SDimitry Andric 
1189489b1cf2SDimitry Andric   return lib_name;
1190489b1cf2SDimitry Andric }
1191489b1cf2SDimitry Andric 
1192*349cc55cSDimitry Andric /* Avoid clashes with std::min */
1193*349cc55cSDimitry Andric #define __itt_min(a, b) ((a) < (b) ? (a) : (b))
1194489b1cf2SDimitry Andric 
__itt_get_groups(void)1195fe6060f1SDimitry Andric static __itt_group_id __itt_get_groups(void) {
1196489b1cf2SDimitry Andric   int i;
1197489b1cf2SDimitry Andric   __itt_group_id res = __itt_group_none;
1198489b1cf2SDimitry Andric   const char *var_name = "INTEL_ITTNOTIFY_GROUPS";
1199489b1cf2SDimitry Andric   const char *group_str = __itt_get_env_var(var_name);
1200489b1cf2SDimitry Andric 
1201fe6060f1SDimitry Andric   if (group_str != NULL) {
1202489b1cf2SDimitry Andric     int len;
1203489b1cf2SDimitry Andric     char gr[255];
1204489b1cf2SDimitry Andric     const char *chunk;
1205fe6060f1SDimitry Andric     while ((group_str = __itt_fsplit(group_str, ",; ", &chunk, &len)) != NULL) {
1206489b1cf2SDimitry Andric       int min_len = __itt_min(len, (int)(sizeof(gr) - 1));
1207489b1cf2SDimitry Andric       __itt_fstrcpyn(gr, sizeof(gr) - 1, chunk, min_len);
1208489b1cf2SDimitry Andric       gr[min_len] = 0;
1209489b1cf2SDimitry Andric 
1210fe6060f1SDimitry Andric       for (i = 0; group_list[i].name != NULL; i++) {
1211fe6060f1SDimitry Andric         if (!__itt_fstrcmp(gr, group_list[i].name)) {
1212489b1cf2SDimitry Andric           res = (__itt_group_id)(res | group_list[i].id);
1213489b1cf2SDimitry Andric           break;
1214489b1cf2SDimitry Andric         }
1215489b1cf2SDimitry Andric       }
1216489b1cf2SDimitry Andric     }
1217489b1cf2SDimitry Andric     /* TODO: !!! Workaround for bug with warning for unknown group !!!
1218489b1cf2SDimitry Andric      * Should be fixed in new initialization scheme.
1219489b1cf2SDimitry Andric      * Now the following groups should be set always. */
1220489b1cf2SDimitry Andric     for (i = 0; group_list[i].id != __itt_group_none; i++)
1221489b1cf2SDimitry Andric       if (group_list[i].id != __itt_group_all &&
1222489b1cf2SDimitry Andric           group_list[i].id > __itt_group_splitter_min &&
1223489b1cf2SDimitry Andric           group_list[i].id < __itt_group_splitter_max)
1224489b1cf2SDimitry Andric         res = (__itt_group_id)(res | group_list[i].id);
1225489b1cf2SDimitry Andric     return res;
1226fe6060f1SDimitry Andric   } else {
1227489b1cf2SDimitry Andric     for (i = 0; group_alias[i].env_var != NULL; i++)
1228489b1cf2SDimitry Andric       if (__itt_get_env_var(group_alias[i].env_var) != NULL)
1229489b1cf2SDimitry Andric         return group_alias[i].groups;
1230489b1cf2SDimitry Andric   }
1231489b1cf2SDimitry Andric 
1232489b1cf2SDimitry Andric   return res;
1233489b1cf2SDimitry Andric }
1234489b1cf2SDimitry Andric 
1235489b1cf2SDimitry Andric #undef __itt_min
1236489b1cf2SDimitry Andric 
__itt_lib_version(lib_t lib)1237fe6060f1SDimitry Andric static int __itt_lib_version(lib_t lib) {
1238489b1cf2SDimitry Andric   if (lib == NULL)
1239489b1cf2SDimitry Andric     return 0;
1240489b1cf2SDimitry Andric   if (__itt_get_proc(lib, "__itt_api_init"))
1241489b1cf2SDimitry Andric     return 2;
1242489b1cf2SDimitry Andric   if (__itt_get_proc(lib, "__itt_api_version"))
1243489b1cf2SDimitry Andric     return 1;
1244489b1cf2SDimitry Andric   return 0;
1245489b1cf2SDimitry Andric }
1246489b1cf2SDimitry Andric 
1247489b1cf2SDimitry Andric /* It's not used right now! Comment it out to avoid warnings.
1248489b1cf2SDimitry Andric static void __itt_reinit_all_pointers(void)
1249489b1cf2SDimitry Andric {
1250*349cc55cSDimitry Andric     register int i;
1251489b1cf2SDimitry Andric     // Fill all pointers with initial stubs
1252489b1cf2SDimitry Andric     for (i = 0; _N_(_ittapi_global).api_list_ptr[i].name != NULL; i++)
1253fe6060f1SDimitry Andric         *_N_(_ittapi_global).api_list_ptr[i].func_ptr =
1254fe6060f1SDimitry Andric _N_(_ittapi_global).api_list_ptr[i].init_func;
1255489b1cf2SDimitry Andric }
1256489b1cf2SDimitry Andric */
1257489b1cf2SDimitry Andric 
__itt_nullify_all_pointers(void)1258fe6060f1SDimitry Andric static void __itt_nullify_all_pointers(void) {
1259489b1cf2SDimitry Andric   int i;
1260*349cc55cSDimitry Andric   /* Nulify all pointers except domain_create, string_handle_create  and
1261fe6060f1SDimitry Andric    * counter_create */
1262489b1cf2SDimitry Andric   for (i = 0; _N_(_ittapi_global).api_list_ptr[i].name != NULL; i++)
1263fe6060f1SDimitry Andric     *_N_(_ittapi_global).api_list_ptr[i].func_ptr =
1264fe6060f1SDimitry Andric         _N_(_ittapi_global).api_list_ptr[i].null_func;
1265489b1cf2SDimitry Andric }
1266489b1cf2SDimitry Andric 
1267*349cc55cSDimitry Andric #if ITT_PLATFORM == ITT_PLATFORM_WIN
1268*349cc55cSDimitry Andric #if _MSC_VER
1269489b1cf2SDimitry Andric #pragma warning(push)
1270*349cc55cSDimitry Andric #pragma warning(disable : 4054) /* warning C4054: 'type cast' : from function  \
1271fe6060f1SDimitry Andric                                    pointer 'XXX' to data pointer 'void *' */
1272*349cc55cSDimitry Andric #pragma warning(                                                               \
1273*349cc55cSDimitry Andric     disable : 4055) /* warning C4055: 'type cast' : from data pointer 'void *' \
1274*349cc55cSDimitry Andric                        to function pointer 'XXX' */
1275*349cc55cSDimitry Andric #endif
1276489b1cf2SDimitry Andric #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
1277489b1cf2SDimitry Andric 
_N_(fini_ittlib)1278fe6060f1SDimitry Andric ITT_EXTERN_C void _N_(fini_ittlib)(void) {
1279489b1cf2SDimitry Andric   __itt_api_fini_t *__itt_api_fini_ptr = NULL;
1280489b1cf2SDimitry Andric   static volatile TIDT current_thread = 0;
1281489b1cf2SDimitry Andric 
1282fe6060f1SDimitry Andric   if (_N_(_ittapi_global).api_initialized) {
1283489b1cf2SDimitry Andric     ITT_MUTEX_INIT_AND_LOCK(_N_(_ittapi_global));
1284fe6060f1SDimitry Andric     if (_N_(_ittapi_global).api_initialized) {
1285fe6060f1SDimitry Andric       if (current_thread == 0) {
1286fe6060f1SDimitry Andric         if (PTHREAD_SYMBOLS)
1287fe6060f1SDimitry Andric           current_thread = __itt_thread_id();
1288fe6060f1SDimitry Andric         if (_N_(_ittapi_global).lib != NULL) {
1289fe6060f1SDimitry Andric           __itt_api_fini_ptr = (__itt_api_fini_t *)(size_t)__itt_get_proc(
1290fe6060f1SDimitry Andric               _N_(_ittapi_global).lib, "__itt_api_fini");
1291489b1cf2SDimitry Andric         }
1292fe6060f1SDimitry Andric         if (__itt_api_fini_ptr) {
1293489b1cf2SDimitry Andric           __itt_api_fini_ptr(&_N_(_ittapi_global));
1294489b1cf2SDimitry Andric         }
1295489b1cf2SDimitry Andric 
1296489b1cf2SDimitry Andric         __itt_nullify_all_pointers();
1297489b1cf2SDimitry Andric 
1298489b1cf2SDimitry Andric         /* TODO: !!! not safe !!! don't support unload so far.
1299489b1cf2SDimitry Andric          *             if (_N_(_ittapi_global).lib != NULL)
1300489b1cf2SDimitry Andric          *                 __itt_unload_lib(_N_(_ittapi_global).lib);
1301489b1cf2SDimitry Andric          *             _N_(_ittapi_global).lib = NULL;
1302489b1cf2SDimitry Andric          */
1303489b1cf2SDimitry Andric         _N_(_ittapi_global).api_initialized = 0;
1304489b1cf2SDimitry Andric         current_thread = 0;
1305489b1cf2SDimitry Andric       }
1306489b1cf2SDimitry Andric     }
1307fe6060f1SDimitry Andric     if (PTHREAD_SYMBOLS)
1308fe6060f1SDimitry Andric       __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
1309489b1cf2SDimitry Andric   }
1310489b1cf2SDimitry Andric }
1311489b1cf2SDimitry Andric 
1312*349cc55cSDimitry Andric /* !!! this function should be called under mutex lock !!! */
__itt_free_allocated_resources(void)1313*349cc55cSDimitry Andric static void __itt_free_allocated_resources(void) {
1314*349cc55cSDimitry Andric   __itt_string_handle *current_string = _N_(_ittapi_global).string_list;
1315*349cc55cSDimitry Andric   while (current_string != NULL) {
1316*349cc55cSDimitry Andric     __itt_string_handle *tmp = current_string->next;
1317*349cc55cSDimitry Andric     free((char *)current_string->strA);
1318*349cc55cSDimitry Andric #if ITT_PLATFORM == ITT_PLATFORM_WIN
1319*349cc55cSDimitry Andric     free((wchar_t *)current_string->strW);
1320*349cc55cSDimitry Andric #endif
1321*349cc55cSDimitry Andric     free(current_string);
1322*349cc55cSDimitry Andric     current_string = tmp;
1323*349cc55cSDimitry Andric   }
1324*349cc55cSDimitry Andric   _N_(_ittapi_global).string_list = NULL;
1325*349cc55cSDimitry Andric 
1326*349cc55cSDimitry Andric   __itt_domain *current_domain = _N_(_ittapi_global).domain_list;
1327*349cc55cSDimitry Andric   while (current_domain != NULL) {
1328*349cc55cSDimitry Andric     __itt_domain *tmp = current_domain->next;
1329*349cc55cSDimitry Andric     free((char *)current_domain->nameA);
1330*349cc55cSDimitry Andric #if ITT_PLATFORM == ITT_PLATFORM_WIN
1331*349cc55cSDimitry Andric     free((wchar_t *)current_domain->nameW);
1332*349cc55cSDimitry Andric #endif
1333*349cc55cSDimitry Andric     free(current_domain);
1334*349cc55cSDimitry Andric     current_domain = tmp;
1335*349cc55cSDimitry Andric   }
1336*349cc55cSDimitry Andric   _N_(_ittapi_global).domain_list = NULL;
1337*349cc55cSDimitry Andric 
1338*349cc55cSDimitry Andric   __itt_counter_info_t *current_couter = _N_(_ittapi_global).counter_list;
1339*349cc55cSDimitry Andric   while (current_couter != NULL) {
1340*349cc55cSDimitry Andric     __itt_counter_info_t *tmp = current_couter->next;
1341*349cc55cSDimitry Andric     free((char *)current_couter->nameA);
1342*349cc55cSDimitry Andric     free((char *)current_couter->domainA);
1343*349cc55cSDimitry Andric #if ITT_PLATFORM == ITT_PLATFORM_WIN
1344*349cc55cSDimitry Andric     free((wchar_t *)current_couter->nameW);
1345*349cc55cSDimitry Andric     free((wchar_t *)current_couter->domainW);
1346*349cc55cSDimitry Andric #endif
1347*349cc55cSDimitry Andric     free(current_couter);
1348*349cc55cSDimitry Andric     current_couter = tmp;
1349*349cc55cSDimitry Andric   }
1350*349cc55cSDimitry Andric   _N_(_ittapi_global).counter_list = NULL;
1351*349cc55cSDimitry Andric 
1352*349cc55cSDimitry Andric   __itt_histogram *current_histogram = _N_(_ittapi_global).histogram_list;
1353*349cc55cSDimitry Andric   while (current_histogram != NULL) {
1354*349cc55cSDimitry Andric     __itt_histogram *tmp = current_histogram->next;
1355*349cc55cSDimitry Andric     free((char *)current_histogram->nameA);
1356*349cc55cSDimitry Andric #if ITT_PLATFORM == ITT_PLATFORM_WIN
1357*349cc55cSDimitry Andric     free((wchar_t *)current_histogram->nameW);
1358*349cc55cSDimitry Andric #endif
1359*349cc55cSDimitry Andric     free(current_histogram);
1360*349cc55cSDimitry Andric     current_histogram = tmp;
1361*349cc55cSDimitry Andric   }
1362*349cc55cSDimitry Andric   _N_(_ittapi_global).histogram_list = NULL;
1363*349cc55cSDimitry Andric }
1364*349cc55cSDimitry Andric 
_N_(init_ittlib)1365fe6060f1SDimitry Andric ITT_EXTERN_C int _N_(init_ittlib)(const char *lib_name,
1366fe6060f1SDimitry Andric                                   __itt_group_id init_groups) {
1367489b1cf2SDimitry Andric   int i;
1368489b1cf2SDimitry Andric   __itt_group_id groups;
1369489b1cf2SDimitry Andric #ifdef ITT_COMPLETE_GROUP
1370489b1cf2SDimitry Andric   __itt_group_id zero_group = __itt_group_none;
1371489b1cf2SDimitry Andric #endif /* ITT_COMPLETE_GROUP */
1372489b1cf2SDimitry Andric   static volatile TIDT current_thread = 0;
1373489b1cf2SDimitry Andric 
1374fe6060f1SDimitry Andric   if (!_N_(_ittapi_global).api_initialized) {
1375489b1cf2SDimitry Andric #ifndef ITT_SIMPLE_INIT
1376489b1cf2SDimitry Andric     ITT_MUTEX_INIT_AND_LOCK(_N_(_ittapi_global));
1377489b1cf2SDimitry Andric #endif /* ITT_SIMPLE_INIT */
1378489b1cf2SDimitry Andric 
1379fe6060f1SDimitry Andric     if (!_N_(_ittapi_global).api_initialized) {
1380fe6060f1SDimitry Andric       if (current_thread == 0) {
1381fe6060f1SDimitry Andric         if (PTHREAD_SYMBOLS)
1382fe6060f1SDimitry Andric           current_thread = __itt_thread_id();
1383fe6060f1SDimitry Andric         if (lib_name == NULL) {
1384489b1cf2SDimitry Andric           lib_name = __itt_get_lib_name();
1385489b1cf2SDimitry Andric         }
1386489b1cf2SDimitry Andric         groups = __itt_get_groups();
1387fe6060f1SDimitry Andric         if (DL_SYMBOLS && (groups != __itt_group_none || lib_name != NULL)) {
1388fe6060f1SDimitry Andric           _N_(_ittapi_global).lib = __itt_load_lib(
1389fe6060f1SDimitry Andric               (lib_name == NULL) ? ittnotify_lib_name : lib_name);
1390489b1cf2SDimitry Andric 
1391fe6060f1SDimitry Andric           if (_N_(_ittapi_global).lib != NULL) {
1392489b1cf2SDimitry Andric             __itt_api_init_t *__itt_api_init_ptr;
1393489b1cf2SDimitry Andric             int lib_version = __itt_lib_version(_N_(_ittapi_global).lib);
1394489b1cf2SDimitry Andric 
1395489b1cf2SDimitry Andric             switch (lib_version) {
1396489b1cf2SDimitry Andric             case 0:
1397489b1cf2SDimitry Andric               groups = __itt_group_legacy;
1398*349cc55cSDimitry Andric               ITT_ATTRIBUTE_FALLTHROUGH;
1399489b1cf2SDimitry Andric             case 1:
1400489b1cf2SDimitry Andric               /* Fill all pointers from dynamic library */
1401fe6060f1SDimitry Andric               for (i = 0; _N_(_ittapi_global).api_list_ptr[i].name != NULL;
1402fe6060f1SDimitry Andric                    i++) {
1403fe6060f1SDimitry Andric                 if (_N_(_ittapi_global).api_list_ptr[i].group & groups &
1404fe6060f1SDimitry Andric                     init_groups) {
1405fe6060f1SDimitry Andric                   *_N_(_ittapi_global).api_list_ptr[i].func_ptr =
1406fe6060f1SDimitry Andric                       (void *)__itt_get_proc(
1407fe6060f1SDimitry Andric                           _N_(_ittapi_global).lib,
1408fe6060f1SDimitry Andric                           _N_(_ittapi_global).api_list_ptr[i].name);
1409fe6060f1SDimitry Andric                   if (*_N_(_ittapi_global).api_list_ptr[i].func_ptr == NULL) {
1410fe6060f1SDimitry Andric                     /* Restore pointers for function with static implementation
1411fe6060f1SDimitry Andric                      */
1412fe6060f1SDimitry Andric                     *_N_(_ittapi_global).api_list_ptr[i].func_ptr =
1413fe6060f1SDimitry Andric                         _N_(_ittapi_global).api_list_ptr[i].null_func;
1414fe6060f1SDimitry Andric                     __itt_report_error(
1415fe6060f1SDimitry Andric                         __itt_error_no_symbol, lib_name,
1416fe6060f1SDimitry Andric                         _N_(_ittapi_global).api_list_ptr[i].name);
1417489b1cf2SDimitry Andric #ifdef ITT_COMPLETE_GROUP
1418*349cc55cSDimitry Andric                     zero_group =
1419*349cc55cSDimitry Andric                         (__itt_group_id)(zero_group | _N_(_ittapi_global)
1420*349cc55cSDimitry Andric                                                           .api_list_ptr[i]
1421*349cc55cSDimitry Andric                                                           .group);
1422489b1cf2SDimitry Andric #endif /* ITT_COMPLETE_GROUP */
1423489b1cf2SDimitry Andric                   }
1424fe6060f1SDimitry Andric                 } else
1425fe6060f1SDimitry Andric                   *_N_(_ittapi_global).api_list_ptr[i].func_ptr =
1426fe6060f1SDimitry Andric                       _N_(_ittapi_global).api_list_ptr[i].null_func;
1427489b1cf2SDimitry Andric               }
1428489b1cf2SDimitry Andric 
1429fe6060f1SDimitry Andric               if (groups == __itt_group_legacy) {
1430489b1cf2SDimitry Andric                 /* Compatibility with legacy tools */
1431489b1cf2SDimitry Andric                 ITTNOTIFY_NAME(thread_ignore) = ITTNOTIFY_NAME(thr_ignore);
1432489b1cf2SDimitry Andric #if ITT_PLATFORM == ITT_PLATFORM_WIN
1433489b1cf2SDimitry Andric                 ITTNOTIFY_NAME(sync_createA) = ITTNOTIFY_NAME(sync_set_nameA);
1434489b1cf2SDimitry Andric                 ITTNOTIFY_NAME(sync_createW) = ITTNOTIFY_NAME(sync_set_nameW);
1435489b1cf2SDimitry Andric #else /* ITT_PLATFORM!=ITT_PLATFORM_WIN */
1436489b1cf2SDimitry Andric                 ITTNOTIFY_NAME(sync_create) = ITTNOTIFY_NAME(sync_set_name);
1437489b1cf2SDimitry Andric #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
1438fe6060f1SDimitry Andric                 ITTNOTIFY_NAME(sync_prepare) =
1439fe6060f1SDimitry Andric                     ITTNOTIFY_NAME(notify_sync_prepare);
1440fe6060f1SDimitry Andric                 ITTNOTIFY_NAME(sync_cancel) =
1441fe6060f1SDimitry Andric                     ITTNOTIFY_NAME(notify_sync_cancel);
1442fe6060f1SDimitry Andric                 ITTNOTIFY_NAME(sync_acquired) =
1443fe6060f1SDimitry Andric                     ITTNOTIFY_NAME(notify_sync_acquired);
1444fe6060f1SDimitry Andric                 ITTNOTIFY_NAME(sync_releasing) =
1445fe6060f1SDimitry Andric                     ITTNOTIFY_NAME(notify_sync_releasing);
1446489b1cf2SDimitry Andric               }
1447489b1cf2SDimitry Andric 
1448489b1cf2SDimitry Andric #ifdef ITT_COMPLETE_GROUP
1449489b1cf2SDimitry Andric               for (i = 0; _N_(_ittapi_global).api_list_ptr[i].name != NULL; i++)
1450489b1cf2SDimitry Andric                 if (_N_(_ittapi_global).api_list_ptr[i].group & zero_group)
1451fe6060f1SDimitry Andric                   *_N_(_ittapi_global).api_list_ptr[i].func_ptr =
1452fe6060f1SDimitry Andric                       _N_(_ittapi_global).api_list_ptr[i].null_func;
1453489b1cf2SDimitry Andric #endif /* ITT_COMPLETE_GROUP */
1454489b1cf2SDimitry Andric               break;
1455489b1cf2SDimitry Andric             case 2:
1456fe6060f1SDimitry Andric               __itt_api_init_ptr = (__itt_api_init_t *)(size_t)__itt_get_proc(
1457fe6060f1SDimitry Andric                   _N_(_ittapi_global).lib, "__itt_api_init");
1458489b1cf2SDimitry Andric               if (__itt_api_init_ptr)
1459489b1cf2SDimitry Andric                 __itt_api_init_ptr(&_N_(_ittapi_global), init_groups);
1460489b1cf2SDimitry Andric               break;
1461489b1cf2SDimitry Andric             }
1462fe6060f1SDimitry Andric           } else {
1463*349cc55cSDimitry Andric             __itt_free_allocated_resources();
1464489b1cf2SDimitry Andric             __itt_nullify_all_pointers();
1465489b1cf2SDimitry Andric 
1466489b1cf2SDimitry Andric             __itt_report_error(__itt_error_no_module, lib_name,
1467489b1cf2SDimitry Andric #if ITT_PLATFORM == ITT_PLATFORM_WIN
1468489b1cf2SDimitry Andric                                __itt_system_error()
1469489b1cf2SDimitry Andric #else /* ITT_PLATFORM==ITT_PLATFORM_WIN */
1470489b1cf2SDimitry Andric                                dlerror()
1471489b1cf2SDimitry Andric #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
1472489b1cf2SDimitry Andric             );
1473489b1cf2SDimitry Andric           }
1474fe6060f1SDimitry Andric         } else {
1475*349cc55cSDimitry Andric           __itt_free_allocated_resources();
1476489b1cf2SDimitry Andric           __itt_nullify_all_pointers();
1477489b1cf2SDimitry Andric         }
1478489b1cf2SDimitry Andric         _N_(_ittapi_global).api_initialized = 1;
1479489b1cf2SDimitry Andric         current_thread = 0;
1480489b1cf2SDimitry Andric         /* !!! Just to avoid unused code elimination !!! */
1481fe6060f1SDimitry Andric         if (__itt_fini_ittlib_ptr == _N_(fini_ittlib))
1482fe6060f1SDimitry Andric           current_thread = 0;
1483489b1cf2SDimitry Andric       }
1484489b1cf2SDimitry Andric     }
1485489b1cf2SDimitry Andric 
1486489b1cf2SDimitry Andric #ifndef ITT_SIMPLE_INIT
1487fe6060f1SDimitry Andric     if (PTHREAD_SYMBOLS)
1488fe6060f1SDimitry Andric       __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
1489489b1cf2SDimitry Andric #endif /* ITT_SIMPLE_INIT */
1490489b1cf2SDimitry Andric   }
1491489b1cf2SDimitry Andric 
1492489b1cf2SDimitry Andric   /* Evaluating if any function ptr is non empty and it's in init_groups */
1493fe6060f1SDimitry Andric   for (i = 0; _N_(_ittapi_global).api_list_ptr[i].name != NULL; i++) {
1494fe6060f1SDimitry Andric     if (*_N_(_ittapi_global).api_list_ptr[i].func_ptr !=
1495fe6060f1SDimitry Andric             _N_(_ittapi_global).api_list_ptr[i].null_func &&
1496fe6060f1SDimitry Andric         _N_(_ittapi_global).api_list_ptr[i].group & init_groups) {
1497489b1cf2SDimitry Andric       return 1;
1498489b1cf2SDimitry Andric     }
1499489b1cf2SDimitry Andric   }
1500489b1cf2SDimitry Andric   return 0;
1501489b1cf2SDimitry Andric }
1502489b1cf2SDimitry Andric 
1503fe6060f1SDimitry Andric ITT_EXTERN_C __itt_error_handler_t *
_N_(set_error_handler)1504fe6060f1SDimitry Andric _N_(set_error_handler)(__itt_error_handler_t *handler) {
1505fe6060f1SDimitry Andric   __itt_error_handler_t *prev =
1506fe6060f1SDimitry Andric       (__itt_error_handler_t *)(size_t)_N_(_ittapi_global).error_handler;
1507489b1cf2SDimitry Andric   _N_(_ittapi_global).error_handler = (void *)(size_t)handler;
1508489b1cf2SDimitry Andric   return prev;
1509489b1cf2SDimitry Andric }
1510489b1cf2SDimitry Andric 
1511*349cc55cSDimitry Andric #if ITT_PLATFORM == ITT_PLATFORM_WIN
1512*349cc55cSDimitry Andric #if _MSC_VER
1513489b1cf2SDimitry Andric #pragma warning(pop)
1514*349cc55cSDimitry Andric #endif
1515489b1cf2SDimitry Andric #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
1516*349cc55cSDimitry Andric 
1517*349cc55cSDimitry Andric /** __itt_mark_pt_region functions marks region of interest
1518*349cc55cSDimitry Andric  * region parameter defines different regions.
1519*349cc55cSDimitry Andric  * 0 <= region < 8 */
1520*349cc55cSDimitry Andric 
1521*349cc55cSDimitry Andric #if defined(ITT_API_IPT_SUPPORT) &&                                            \
1522*349cc55cSDimitry Andric     (ITT_PLATFORM == ITT_PLATFORM_WIN ||                                       \
1523*349cc55cSDimitry Andric      ITT_PLATFORM == ITT_PLATFORM_POSIX) &&                                    \
1524*349cc55cSDimitry Andric     !defined(__ANDROID__)
1525*349cc55cSDimitry Andric void __itt_pt_mark(__itt_pt_region region);
1526*349cc55cSDimitry Andric void __itt_pt_mark_event(__itt_pt_region region);
1527*349cc55cSDimitry Andric #endif
1528*349cc55cSDimitry Andric 
_N_(mark_pt_region_begin)1529*349cc55cSDimitry Andric ITT_EXTERN_C void _N_(mark_pt_region_begin)(__itt_pt_region region) {
1530*349cc55cSDimitry Andric #if defined(ITT_API_IPT_SUPPORT) &&                                            \
1531*349cc55cSDimitry Andric     (ITT_PLATFORM == ITT_PLATFORM_WIN ||                                       \
1532*349cc55cSDimitry Andric      ITT_PLATFORM == ITT_PLATFORM_POSIX) &&                                    \
1533*349cc55cSDimitry Andric     !defined(__ANDROID__)
1534*349cc55cSDimitry Andric   if (_N_(_ittapi_global).ipt_collect_events == 1) {
1535*349cc55cSDimitry Andric     __itt_pt_mark_event(2 * region);
1536*349cc55cSDimitry Andric   } else {
1537*349cc55cSDimitry Andric     __itt_pt_mark(2 * region);
1538*349cc55cSDimitry Andric   }
1539*349cc55cSDimitry Andric #else
1540*349cc55cSDimitry Andric   (void)region;
1541*349cc55cSDimitry Andric #endif
1542*349cc55cSDimitry Andric }
1543*349cc55cSDimitry Andric 
_N_(mark_pt_region_end)1544*349cc55cSDimitry Andric ITT_EXTERN_C void _N_(mark_pt_region_end)(__itt_pt_region region) {
1545*349cc55cSDimitry Andric #if defined(ITT_API_IPT_SUPPORT) &&                                            \
1546*349cc55cSDimitry Andric     (ITT_PLATFORM == ITT_PLATFORM_WIN ||                                       \
1547*349cc55cSDimitry Andric      ITT_PLATFORM == ITT_PLATFORM_POSIX) &&                                    \
1548*349cc55cSDimitry Andric     !defined(__ANDROID__)
1549*349cc55cSDimitry Andric   if (_N_(_ittapi_global).ipt_collect_events == 1) {
1550*349cc55cSDimitry Andric     __itt_pt_mark_event(2 * region + 1);
1551*349cc55cSDimitry Andric   } else {
1552*349cc55cSDimitry Andric     __itt_pt_mark(2 * region + 1);
1553*349cc55cSDimitry Andric   }
1554*349cc55cSDimitry Andric #else
1555*349cc55cSDimitry Andric   (void)region;
1556*349cc55cSDimitry Andric #endif
1557*349cc55cSDimitry Andric }
1558