xref: /oneTBB/src/tbb/tools_api/ittnotify_static.c (revision bb45f092)
1 /*
2     Copyright (c) 2005-2022 Intel Corporation
3 
4     Licensed under the Apache License, Version 2.0 (the "License");
5     you may not use this file except in compliance with the License.
6     You may obtain a copy of the License at
7 
8         http://www.apache.org/licenses/LICENSE-2.0
9 
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15 */
16 
17 #define INTEL_NO_MACRO_BODY
18 #define INTEL_ITTNOTIFY_API_PRIVATE
19 #include "ittnotify_config.h"
20 
21 #if ITT_PLATFORM==ITT_PLATFORM_WIN
22 #if !defined(PATH_MAX)
23 #define PATH_MAX 512
24 #endif
25 #else /* ITT_PLATFORM!=ITT_PLATFORM_WIN */
26 #include <limits.h>
27 #include <dlfcn.h>
28 #include <errno.h>
29 #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <stdarg.h>
33 #include <string.h>
34 
35 #include "ittnotify.h"
36 #include "legacy/ittnotify.h"
37 
38 #include "disable_warnings.h"
39 
40 static const char api_version[] = API_VERSION "\0\n@(#) $Revision$\n";
41 
42 #define _N_(n) ITT_JOIN(INTEL_ITTNOTIFY_PREFIX,n)
43 
44 #ifndef HAS_CPP_ATTR
45 #if defined(__cplusplus) && defined(__has_cpp_attribute)
46 #define HAS_CPP_ATTR(X) __has_cpp_attribute(X)
47 #else
48 #define HAS_CPP_ATTR(X) 0
49 #endif
50 #endif
51 
52 #ifndef HAS_C_ATTR
53 #if defined(__STDC__) && defined(__has_c_attribute)
54 #define HAS_C_ATTR(X) __has_c_attribute(X)
55 #else
56 #define HAS_C_ATTR(X) 0
57 #endif
58 #endif
59 
60 #ifndef HAS_GNU_ATTR
61 #if defined(__has_attribute)
62 #define HAS_GNU_ATTR(X) __has_attribute(X)
63 #else
64 #define HAS_GNU_ATTR(X) 0
65 #endif
66 #endif
67 
68 #ifndef ITT_ATTRIBUTE_FALLTHROUGH
69 #if (HAS_CPP_ATTR(fallthrough) || HAS_C_ATTR(fallthrough)) && (__cplusplus >= 201703L || _MSVC_LANG >= 201703L)
70 #define ITT_ATTRIBUTE_FALLTHROUGH [[fallthrough]]
71 #elif HAS_CPP_ATTR(gnu::fallthrough)
72 #define ITT_ATTRIBUTE_FALLTHROUGH [[gnu::fallthrough]]
73 #elif HAS_CPP_ATTR(clang::fallthrough)
74 #define ITT_ATTRIBUTE_FALLTHROUGH [[clang::fallthrough]]
75 #elif HAS_GNU_ATTR(fallthrough) && !__INTEL_COMPILER
76 #define ITT_ATTRIBUTE_FALLTHROUGH __attribute__((fallthrough))
77 #else
78 #define ITT_ATTRIBUTE_FALLTHROUGH
79 #endif
80 #endif
81 
82 #if ITT_OS==ITT_OS_WIN
83 static const char* ittnotify_lib_name = "libittnotify.dll";
84 #elif ITT_OS==ITT_OS_LINUX || ITT_OS==ITT_OS_FREEBSD
85 static const char* ittnotify_lib_name = "libittnotify.so";
86 #elif ITT_OS==ITT_OS_MAC
87 static const char* ittnotify_lib_name = "libittnotify.dylib";
88 #else
89 #error Unsupported or unknown OS.
90 #endif
91 
92 #ifdef __ANDROID__
93 #include <android/log.h>
94 #include <stdio.h>
95 #include <unistd.h>
96 #include <sys/types.h>
97 #include <sys/stat.h>
98 #include <fcntl.h>
99 #include <linux/limits.h>
100 
101 #ifdef ITT_ANDROID_LOG
102     #define ITT_ANDROID_LOG_TAG   "INTEL_VTUNE_USERAPI"
103     #define ITT_ANDROID_LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, ITT_ANDROID_LOG_TAG, __VA_ARGS__))
104     #define ITT_ANDROID_LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN, ITT_ANDROID_LOG_TAG, __VA_ARGS__))
105     #define ITT_ANDROID_LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR,ITT_ANDROID_LOG_TAG, __VA_ARGS__))
106     #define ITT_ANDROID_LOGD(...) ((void)__android_log_print(ANDROID_LOG_DEBUG,ITT_ANDROID_LOG_TAG, __VA_ARGS__))
107 #else
108     #define ITT_ANDROID_LOGI(...)
109     #define ITT_ANDROID_LOGW(...)
110     #define ITT_ANDROID_LOGE(...)
111     #define ITT_ANDROID_LOGD(...)
112 #endif
113 
114 /* default location of userapi collector on Android */
115 #define ANDROID_ITTNOTIFY_DEFAULT_PATH_MASK(x)  "/data/data/com.intel.vtune/perfrun/lib" \
116                                                 #x "/runtime/libittnotify.so"
117 
118 #if ITT_ARCH==ITT_ARCH_IA32 || ITT_ARCH==ITT_ARCH_ARM
119 #define ANDROID_ITTNOTIFY_DEFAULT_PATH  ANDROID_ITTNOTIFY_DEFAULT_PATH_MASK(32)
120 #else
121 #define ANDROID_ITTNOTIFY_DEFAULT_PATH  ANDROID_ITTNOTIFY_DEFAULT_PATH_MASK(64)
122 #endif
123 
124 #endif
125 
126 
127 #ifndef LIB_VAR_NAME
128 #if ITT_ARCH==ITT_ARCH_IA32 || ITT_ARCH==ITT_ARCH_ARM
129 #define LIB_VAR_NAME INTEL_LIBITTNOTIFY32
130 #else
131 #define LIB_VAR_NAME INTEL_LIBITTNOTIFY64
132 #endif
133 #endif /* LIB_VAR_NAME */
134 
135 #define ITT_MUTEX_INIT_AND_LOCK(p) {                                 \
136     if (PTHREAD_SYMBOLS)                                             \
137     {                                                                \
138         if (!p.mutex_initialized)                                    \
139         {                                                            \
140             if (__itt_interlocked_compare_exchange(&p.atomic_counter, 1, 0) == 0) \
141             {                                                        \
142                 __itt_mutex_init(&p.mutex);                          \
143                 p.mutex_initialized = 1;                             \
144             }                                                        \
145             else                                                     \
146                 while (!p.mutex_initialized)                         \
147                     __itt_thread_yield();                            \
148         }                                                            \
149         __itt_mutex_lock(&p.mutex);                                  \
150     }                                                                \
151 }
152 
153 #define ITT_MUTEX_DESTROY(p) {                                       \
154     if (PTHREAD_SYMBOLS)                                             \
155     {                                                                \
156         if (p.mutex_initialized)                                     \
157         {                                                            \
158             if (__itt_interlocked_compare_exchange(&p.atomic_counter, 0, 1) == 1) \
159             {                                                        \
160                 __itt_mutex_destroy(&p.mutex);                       \
161                 p.mutex_initialized = 0;                             \
162             }                                                        \
163         }                                                            \
164     }                                                                \
165 }
166 
167 #define ITT_MODULE_OBJECT_VERSION 1
168 
169 typedef int (__itt_init_ittlib_t)(const char*, __itt_group_id);
170 
171 /* this define used to control initialization function name. */
172 #ifndef __itt_init_ittlib_name
173 ITT_EXTERN_C int _N_(init_ittlib)(const char*, __itt_group_id);
174 static __itt_init_ittlib_t* __itt_init_ittlib_ptr = _N_(init_ittlib);
175 #define __itt_init_ittlib_name __itt_init_ittlib_ptr
176 #endif /* __itt_init_ittlib_name */
177 
178 typedef void (__itt_fini_ittlib_t)(void);
179 
180 /* this define used to control finalization function name. */
181 #ifndef __itt_fini_ittlib_name
182 ITT_EXTERN_C void _N_(fini_ittlib)(void);
183 static __itt_fini_ittlib_t* __itt_fini_ittlib_ptr = _N_(fini_ittlib);
184 #define __itt_fini_ittlib_name __itt_fini_ittlib_ptr
185 #endif /* __itt_fini_ittlib_name */
186 
187 extern __itt_global _N_(_ittapi_global);
188 
189 /* building pointers to imported funcs */
190 #undef ITT_STUBV
191 #undef ITT_STUB
192 #define ITT_STUB(api,type,name,args,params,ptr,group,format)   \
193 static type api ITT_VERSIONIZE(ITT_JOIN(_N_(name),_init)) args;\
194 typedef type api ITT_JOIN(_N_(name),_t) args;                  \
195 ITT_EXTERN_C_BEGIN ITT_JOIN(_N_(name),_t)* ITTNOTIFY_NAME(name) = ITT_VERSIONIZE(ITT_JOIN(_N_(name),_init)); ITT_EXTERN_C_END \
196 static type api ITT_VERSIONIZE(ITT_JOIN(_N_(name),_init)) args \
197 {                                                              \
198     if (!_N_(_ittapi_global).api_initialized && _N_(_ittapi_global).thread_list == NULL) \
199         __itt_init_ittlib_name(NULL, __itt_group_all);         \
200     if (ITTNOTIFY_NAME(name) && ITTNOTIFY_NAME(name) != ITT_VERSIONIZE(ITT_JOIN(_N_(name),_init))) \
201         return ITTNOTIFY_NAME(name) params;                    \
202     else                                                       \
203         return (type)0;                                        \
204 }
205 
206 #define ITT_STUBV(api,type,name,args,params,ptr,group,format)  \
207 static type api ITT_VERSIONIZE(ITT_JOIN(_N_(name),_init)) args;\
208 typedef type api ITT_JOIN(_N_(name),_t) args;                  \
209 ITT_EXTERN_C_BEGIN ITT_JOIN(_N_(name),_t)* ITTNOTIFY_NAME(name) = ITT_VERSIONIZE(ITT_JOIN(_N_(name),_init)); ITT_EXTERN_C_END \
210 static type api ITT_VERSIONIZE(ITT_JOIN(_N_(name),_init)) args \
211 {                                                              \
212     if (!_N_(_ittapi_global).api_initialized && _N_(_ittapi_global).thread_list == NULL) \
213         __itt_init_ittlib_name(NULL, __itt_group_all);         \
214     if (ITTNOTIFY_NAME(name) && ITTNOTIFY_NAME(name) != ITT_VERSIONIZE(ITT_JOIN(_N_(name),_init))) \
215         ITTNOTIFY_NAME(name) params;                           \
216     else                                                       \
217         return;                                                \
218 }
219 
220 #undef __ITT_INTERNAL_INIT
221 #include "ittnotify_static.h"
222 
223 #undef ITT_STUB
224 #undef ITT_STUBV
225 #define ITT_STUB(api,type,name,args,params,ptr,group,format)   \
226 static type api ITT_VERSIONIZE(ITT_JOIN(_N_(name),_init)) args;\
227 typedef type api ITT_JOIN(_N_(name),_t) args;                  \
228 ITT_EXTERN_C_BEGIN ITT_JOIN(_N_(name),_t)* ITTNOTIFY_NAME(name) = ITT_VERSIONIZE(ITT_JOIN(_N_(name),_init)); ITT_EXTERN_C_END
229 
230 #define ITT_STUBV(api,type,name,args,params,ptr,group,format)  \
231 static type api ITT_VERSIONIZE(ITT_JOIN(_N_(name),_init)) args;\
232 typedef type api ITT_JOIN(_N_(name),_t) args;                  \
233 ITT_EXTERN_C_BEGIN ITT_JOIN(_N_(name),_t)* ITTNOTIFY_NAME(name) = ITT_VERSIONIZE(ITT_JOIN(_N_(name),_init)); ITT_EXTERN_C_END
234 
235 #define __ITT_INTERNAL_INIT
236 #include "ittnotify_static.h"
237 #undef __ITT_INTERNAL_INIT
238 
239 ITT_GROUP_LIST(group_list);
240 
241 #pragma pack(push, 8)
242 
243 typedef struct ___itt_group_alias
244 {
245     const char*    env_var;
246     __itt_group_id groups;
247 } __itt_group_alias;
248 
249 static __itt_group_alias group_alias[] = {
250     { "KMP_FOR_TPROFILE", (__itt_group_id)(__itt_group_control | __itt_group_thread | __itt_group_sync  | __itt_group_mark) },
251     { "KMP_FOR_TCHECK",   (__itt_group_id)(__itt_group_control | __itt_group_thread | __itt_group_sync  | __itt_group_fsync | __itt_group_mark | __itt_group_suppress) },
252     { NULL,               (__itt_group_none) },
253     { api_version,        (__itt_group_none) } /* !!! Just to avoid unused code elimination !!! */
254 };
255 
256 #pragma pack(pop)
257 
258 #if ITT_PLATFORM==ITT_PLATFORM_WIN
259 #if _MSC_VER
260 #pragma warning(push)
261 #pragma warning(disable: 4054) /* warning C4054: 'type cast' : from function pointer 'XXX' to data pointer 'void *' */
262 #endif
263 #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
264 
265 static __itt_api_info api_list[] = {
266 /* Define functions with static implementation */
267 #undef ITT_STUB
268 #undef ITT_STUBV
269 #define ITT_STUB(api,type,name,args,params,nameindll,group,format) { ITT_TO_STR(ITT_JOIN(__itt_,nameindll)), (void**)(void*)&ITTNOTIFY_NAME(name), (void*)(size_t)&ITT_VERSIONIZE(ITT_JOIN(_N_(name),_init)), (void*)(size_t)&ITT_VERSIONIZE(ITT_JOIN(_N_(name),_init)), (__itt_group_id)(group)},
270 #define ITT_STUBV ITT_STUB
271 #define __ITT_INTERNAL_INIT
272 #include "ittnotify_static.h"
273 #undef __ITT_INTERNAL_INIT
274 /* Define functions without static implementation */
275 #undef ITT_STUB
276 #undef ITT_STUBV
277 #define ITT_STUB(api,type,name,args,params,nameindll,group,format) {ITT_TO_STR(ITT_JOIN(__itt_,nameindll)), (void**)(void*)&ITTNOTIFY_NAME(name), (void*)(size_t)&ITT_VERSIONIZE(ITT_JOIN(_N_(name),_init)), NULL, (__itt_group_id)(group)},
278 #define ITT_STUBV ITT_STUB
279 #include "ittnotify_static.h"
280     {NULL, NULL, NULL, NULL, __itt_group_none}
281 };
282 
283 #if ITT_PLATFORM==ITT_PLATFORM_WIN
284 #if _MSC_VER
285 #pragma warning(pop)
286 #endif
287 #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
288 
289 /* static part descriptor which handles. all notification api attributes. */
290 __itt_global _N_(_ittapi_global) = {
291     ITT_MAGIC,                                     /* identification info */
292     ITT_MAJOR, ITT_MINOR, API_VERSION_BUILD,       /* version info */
293     0,                                             /* api_initialized */
294     0,                                             /* mutex_initialized */
295     0,                                             /* atomic_counter */
296     MUTEX_INITIALIZER,                             /* mutex */
297     NULL,                                          /* dynamic library handle */
298     NULL,                                          /* error_handler */
299     NULL,                                          /* dll_path_ptr */
300     (__itt_api_info*)&api_list,                    /* api_list_ptr */
301     NULL,                                          /* next __itt_global */
302     NULL,                                          /* thread_list */
303     NULL,                                          /* domain_list */
304     NULL,                                          /* string_list */
305     __itt_collection_uninitialized,                /* collection state */
306     NULL,                                          /* counter_list */
307     0,                                             /* ipt_collect_events */
308     NULL                                           /* histogram_list */
309 };
310 
311 typedef void (__itt_api_init_t)(__itt_global*, __itt_group_id);
312 typedef void (__itt_api_fini_t)(__itt_global*);
313 
314 static __itt_domain dummy_domain;
315 /* ========================================================================= */
316 
317 #ifdef ITT_NOTIFY_EXT_REPORT
318 ITT_EXTERN_C void _N_(error_handler)(__itt_error_code, va_list args);
319 #endif /* ITT_NOTIFY_EXT_REPORT */
320 
321 #if ITT_PLATFORM==ITT_PLATFORM_WIN
322 #if _MSC_VER
323 #pragma warning(push)
324 #pragma warning(disable: 4055) /* warning C4055: 'type cast' : from data pointer 'void *' to function pointer 'XXX' */
325 #endif
326 #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
327 
328 static void __itt_report_error(int code, ...)
329 {
330     va_list args;
331     va_start(args, code);
332     if (_N_(_ittapi_global).error_handler != NULL)
333     {
334         __itt_error_handler_t* handler = (__itt_error_handler_t*)(size_t)_N_(_ittapi_global).error_handler;
335         handler((__itt_error_code)code, args);
336     }
337 #ifdef ITT_NOTIFY_EXT_REPORT
338     _N_(error_handler)((__itt_error_code)code, args);
339 #endif /* ITT_NOTIFY_EXT_REPORT */
340     va_end(args);
341 }
342 
343 static int __itt_is_collector_available(void);
344 
345 #if ITT_PLATFORM==ITT_PLATFORM_WIN
346 #if _MSC_VER
347 #pragma warning(pop)
348 #endif
349 #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
350 
351 #if ITT_PLATFORM==ITT_PLATFORM_WIN
352 static __itt_domain* ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(domain_createW),_init))(const wchar_t* name)
353 {
354     __itt_domain *h_tail = NULL, *h = NULL;
355 
356     if (name == NULL)
357     {
358         return NULL;
359     }
360 
361     ITT_MUTEX_INIT_AND_LOCK(_N_(_ittapi_global));
362     if (_N_(_ittapi_global).api_initialized)
363     {
364         if (ITTNOTIFY_NAME(domain_createW) && ITTNOTIFY_NAME(domain_createW) != ITT_VERSIONIZE(ITT_JOIN(_N_(domain_createW),_init)))
365         {
366             __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
367             return ITTNOTIFY_NAME(domain_createW)(name);
368         }
369         else
370         {
371             __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
372             return &dummy_domain;
373         }
374     }
375     if (__itt_is_collector_available())
376     {
377         for (h_tail = NULL, h = _N_(_ittapi_global).domain_list; h != NULL; h_tail = h, h = h->next)
378         {
379             if (h->nameW != NULL && !wcscmp(h->nameW, name)) break;
380         }
381         if (h == NULL)
382         {
383             NEW_DOMAIN_W(&_N_(_ittapi_global), h, h_tail, name);
384         }
385     }
386     if (PTHREAD_SYMBOLS) __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
387     return h;
388 }
389 
390 static __itt_domain* ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(domain_createA),_init))(const char* name)
391 #else  /* ITT_PLATFORM!=ITT_PLATFORM_WIN */
392 static __itt_domain* ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(domain_create),_init))(const char* name)
393 #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
394 {
395     __itt_domain *h_tail = NULL, *h = NULL;
396 
397     if (name == NULL)
398     {
399         return NULL;
400     }
401 
402     ITT_MUTEX_INIT_AND_LOCK(_N_(_ittapi_global));
403     if (_N_(_ittapi_global).api_initialized)
404     {
405 #if ITT_PLATFORM==ITT_PLATFORM_WIN
406         if (ITTNOTIFY_NAME(domain_createA) && ITTNOTIFY_NAME(domain_createA) != ITT_VERSIONIZE(ITT_JOIN(_N_(domain_createA),_init)))
407         {
408             __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
409             return ITTNOTIFY_NAME(domain_createA)(name);
410         }
411 #else
412         if (ITTNOTIFY_NAME(domain_create) && ITTNOTIFY_NAME(domain_create) != ITT_VERSIONIZE(ITT_JOIN(_N_(domain_create),_init)))
413         {
414             if (PTHREAD_SYMBOLS) __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
415             return ITTNOTIFY_NAME(domain_create)(name);
416         }
417 #endif
418         else
419         {
420 #if ITT_PLATFORM==ITT_PLATFORM_WIN
421             __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
422 #else
423             if (PTHREAD_SYMBOLS) __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
424 #endif
425             return &dummy_domain;
426         }
427     }
428     if (__itt_is_collector_available())
429     {
430         for (h_tail = NULL, h = _N_(_ittapi_global).domain_list; h != NULL; h_tail = h, h = h->next)
431         {
432             if (h->nameA != NULL && !__itt_fstrcmp(h->nameA, name)) break;
433         }
434         if (h == NULL)
435         {
436             NEW_DOMAIN_A(&_N_(_ittapi_global), h, h_tail, name);
437         }
438     }
439     if (PTHREAD_SYMBOLS) __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
440     return h;
441 }
442 
443 static void ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(module_load_with_sections),_init))(__itt_module_object* module_obj)
444 {
445     if (!_N_(_ittapi_global).api_initialized && _N_(_ittapi_global).thread_list == NULL)
446     {
447         __itt_init_ittlib_name(NULL, __itt_group_all);
448     }
449     if (ITTNOTIFY_NAME(module_load_with_sections) && ITTNOTIFY_NAME(module_load_with_sections) != ITT_VERSIONIZE(ITT_JOIN(_N_(module_load_with_sections),_init)))
450     {
451         if(module_obj != NULL)
452         {
453             module_obj->version = ITT_MODULE_OBJECT_VERSION;
454             ITTNOTIFY_NAME(module_load_with_sections)(module_obj);
455         }
456     }
457 }
458 
459 static void ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(module_unload_with_sections),_init))(__itt_module_object* module_obj)
460 {
461     if (!_N_(_ittapi_global).api_initialized && _N_(_ittapi_global).thread_list == NULL)
462     {
463         __itt_init_ittlib_name(NULL, __itt_group_all);
464     }
465     if (ITTNOTIFY_NAME(module_unload_with_sections) && ITTNOTIFY_NAME(module_unload_with_sections) != ITT_VERSIONIZE(ITT_JOIN(_N_(module_unload_with_sections),_init)))
466     {
467         if(module_obj != NULL)
468         {
469             module_obj->version = ITT_MODULE_OBJECT_VERSION;
470             ITTNOTIFY_NAME(module_unload_with_sections)(module_obj);
471         }
472     }
473 }
474 
475 #if ITT_PLATFORM==ITT_PLATFORM_WIN
476 static __itt_string_handle* ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(string_handle_createW),_init))(const wchar_t* name)
477 {
478     __itt_string_handle *h_tail = NULL, *h = NULL;
479 
480     if (name == NULL)
481     {
482         return NULL;
483     }
484 
485     ITT_MUTEX_INIT_AND_LOCK(_N_(_ittapi_global));
486     if (_N_(_ittapi_global).api_initialized)
487     {
488         if (ITTNOTIFY_NAME(string_handle_createW) && ITTNOTIFY_NAME(string_handle_createW) != ITT_VERSIONIZE(ITT_JOIN(_N_(string_handle_createW),_init)))
489         {
490             __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
491             return ITTNOTIFY_NAME(string_handle_createW)(name);
492         }
493         else
494         {
495             __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
496             return NULL;
497         }
498     }
499     if (__itt_is_collector_available())
500     {
501         for (h_tail = NULL, h = _N_(_ittapi_global).string_list; h != NULL; h_tail = h, h = h->next)
502         {
503             if (h->strW != NULL && !wcscmp(h->strW, name)) break;
504         }
505         if (h == NULL)
506         {
507             NEW_STRING_HANDLE_W(&_N_(_ittapi_global), h, h_tail, name);
508         }
509     }
510     __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
511     return h;
512 }
513 
514 static __itt_string_handle* ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(string_handle_createA),_init))(const char* name)
515 #else  /* ITT_PLATFORM!=ITT_PLATFORM_WIN */
516 static __itt_string_handle* ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(string_handle_create),_init))(const char* name)
517 #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
518 {
519     __itt_string_handle *h_tail = NULL, *h = NULL;
520 
521     if (name == NULL)
522     {
523         return NULL;
524     }
525 
526     ITT_MUTEX_INIT_AND_LOCK(_N_(_ittapi_global));
527     if (_N_(_ittapi_global).api_initialized)
528     {
529 #if ITT_PLATFORM==ITT_PLATFORM_WIN
530         if (ITTNOTIFY_NAME(string_handle_createA) && ITTNOTIFY_NAME(string_handle_createA) != ITT_VERSIONIZE(ITT_JOIN(_N_(string_handle_createA),_init)))
531         {
532             __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
533             return ITTNOTIFY_NAME(string_handle_createA)(name);
534         }
535 #else
536         if (ITTNOTIFY_NAME(string_handle_create) && ITTNOTIFY_NAME(string_handle_create) != ITT_VERSIONIZE(ITT_JOIN(_N_(string_handle_create),_init)))
537         {
538             if (PTHREAD_SYMBOLS) __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
539             return ITTNOTIFY_NAME(string_handle_create)(name);
540         }
541 #endif
542         else
543         {
544 #if ITT_PLATFORM==ITT_PLATFORM_WIN
545             __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
546 #else
547             if (PTHREAD_SYMBOLS) __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
548 #endif
549             return NULL;
550         }
551     }
552     if (__itt_is_collector_available())
553     {
554         for (h_tail = NULL, h = _N_(_ittapi_global).string_list; h != NULL; h_tail = h, h = h->next)
555         {
556             if (h->strA != NULL && !__itt_fstrcmp(h->strA, name)) break;
557         }
558         if (h == NULL)
559         {
560             NEW_STRING_HANDLE_A(&_N_(_ittapi_global), h, h_tail, name);
561         }
562     }
563     if (PTHREAD_SYMBOLS) __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
564     return h;
565 }
566 
567 #if ITT_PLATFORM==ITT_PLATFORM_WIN
568 static __itt_counter ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(counter_createW),_init))(const wchar_t *name, const wchar_t *domain)
569 {
570     __itt_counter_info_t *h_tail = NULL, *h = NULL;
571     __itt_metadata_type type = __itt_metadata_u64;
572 
573     if (name == NULL)
574     {
575         return NULL;
576     }
577 
578     ITT_MUTEX_INIT_AND_LOCK(_N_(_ittapi_global));
579     if (_N_(_ittapi_global).api_initialized)
580     {
581         if (ITTNOTIFY_NAME(counter_createW) && ITTNOTIFY_NAME(counter_createW) != ITT_VERSIONIZE(ITT_JOIN(_N_(counter_createW),_init)))
582         {
583             __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
584             return ITTNOTIFY_NAME(counter_createW)(name, domain);
585         }
586         else
587         {
588             __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
589             return NULL;
590         }
591     }
592     if (__itt_is_collector_available())
593     {
594         for (h_tail = NULL, h = _N_(_ittapi_global).counter_list; h != NULL; h_tail = h, h = h->next)
595         {
596             if (h->nameW != NULL && h->type == (int)type && !wcscmp(h->nameW, name) && ((h->domainW == NULL && domain == NULL) ||
597                 (h->domainW != NULL && domain != NULL && !wcscmp(h->domainW, domain)))) break;
598 
599         }
600         if (h == NULL)
601         {
602             NEW_COUNTER_W(&_N_(_ittapi_global), h, h_tail, name, domain, type);
603         }
604     }
605     __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
606     return (__itt_counter)h;
607 }
608 
609 static __itt_counter ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(counter_createA),_init))(const char *name, const char *domain)
610 #else  /* ITT_PLATFORM!=ITT_PLATFORM_WIN */
611 static __itt_counter ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(counter_create),_init))(const char *name, const char *domain)
612 #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
613 {
614     __itt_counter_info_t *h_tail = NULL, *h = NULL;
615     __itt_metadata_type type = __itt_metadata_u64;
616 
617     if (name == NULL)
618     {
619         return NULL;
620     }
621 
622     ITT_MUTEX_INIT_AND_LOCK(_N_(_ittapi_global));
623     if (_N_(_ittapi_global).api_initialized)
624     {
625 #if ITT_PLATFORM==ITT_PLATFORM_WIN
626         if (ITTNOTIFY_NAME(counter_createA) && ITTNOTIFY_NAME(counter_createA) != ITT_VERSIONIZE(ITT_JOIN(_N_(counter_createA),_init)))
627         {
628             __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
629             return ITTNOTIFY_NAME(counter_createA)(name, domain);
630         }
631 #else
632         if (ITTNOTIFY_NAME(counter_create) && ITTNOTIFY_NAME(counter_create) != ITT_VERSIONIZE(ITT_JOIN(_N_(counter_create),_init)))
633         {
634             if (PTHREAD_SYMBOLS) __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
635             return ITTNOTIFY_NAME(counter_create)(name, domain);
636         }
637 #endif
638         else
639         {
640 #if ITT_PLATFORM==ITT_PLATFORM_WIN
641             __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
642 #else
643             if (PTHREAD_SYMBOLS) __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
644 #endif
645             return NULL;
646         }
647     }
648     if (__itt_is_collector_available())
649     {
650         for (h_tail = NULL, h = _N_(_ittapi_global).counter_list; h != NULL; h_tail = h, h = h->next)
651         {
652             if (h->nameA != NULL && h->type == (int)type && !__itt_fstrcmp(h->nameA, name) && ((h->domainA == NULL && domain == NULL) ||
653                 (h->domainA != NULL && domain != NULL && !__itt_fstrcmp(h->domainA, domain)))) break;
654         }
655         if (h == NULL)
656         {
657             NEW_COUNTER_A(&_N_(_ittapi_global), h, h_tail, name, domain, type);
658         }
659     }
660     if (PTHREAD_SYMBOLS) __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
661     return (__itt_counter)h;
662 }
663 
664 #if ITT_PLATFORM==ITT_PLATFORM_WIN
665 static __itt_counter ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(counter_create_typedW),_init))(const wchar_t *name, const wchar_t *domain, __itt_metadata_type type)
666 {
667     __itt_counter_info_t *h_tail = NULL, *h = NULL;
668 
669     if (name == NULL)
670     {
671         return NULL;
672     }
673 
674     ITT_MUTEX_INIT_AND_LOCK(_N_(_ittapi_global));
675     if (_N_(_ittapi_global).api_initialized)
676     {
677         if (ITTNOTIFY_NAME(counter_create_typedW) && ITTNOTIFY_NAME(counter_create_typedW) != ITT_VERSIONIZE(ITT_JOIN(_N_(counter_create_typedW),_init)))
678         {
679             __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
680             return ITTNOTIFY_NAME(counter_create_typedW)(name, domain, type);
681         }
682         else
683         {
684             __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
685             return NULL;
686         }
687     }
688     if (__itt_is_collector_available())
689     {
690         for (h_tail = NULL, h = _N_(_ittapi_global).counter_list; h != NULL; h_tail = h, h = h->next)
691         {
692             if (h->nameW != NULL && h->type == (int)type && !wcscmp(h->nameW, name) && ((h->domainW == NULL && domain == NULL) ||
693                 (h->domainW != NULL && domain != NULL && !wcscmp(h->domainW, domain)))) break;
694 
695         }
696         if (h == NULL)
697         {
698             NEW_COUNTER_W(&_N_(_ittapi_global), h, h_tail, name, domain, type);
699         }
700     }
701     __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
702     return (__itt_counter)h;
703 }
704 
705 static __itt_counter ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(counter_create_typedA),_init))(const char *name, const char *domain, __itt_metadata_type type)
706 #else  /* ITT_PLATFORM!=ITT_PLATFORM_WIN */
707 static __itt_counter ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(counter_create_typed),_init))(const char *name, const char *domain, __itt_metadata_type type)
708 #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
709 {
710     __itt_counter_info_t *h_tail = NULL, *h = NULL;
711 
712     if (name == NULL)
713     {
714         return NULL;
715     }
716 
717     ITT_MUTEX_INIT_AND_LOCK(_N_(_ittapi_global));
718     if (_N_(_ittapi_global).api_initialized)
719     {
720 #if ITT_PLATFORM==ITT_PLATFORM_WIN
721         if (ITTNOTIFY_NAME(counter_create_typedA) && ITTNOTIFY_NAME(counter_create_typedA) != ITT_VERSIONIZE(ITT_JOIN(_N_(counter_create_typedA),_init)))
722         {
723             __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
724             return ITTNOTIFY_NAME(counter_create_typedA)(name, domain, type);
725         }
726 #else
727         if (ITTNOTIFY_NAME(counter_create_typed) && ITTNOTIFY_NAME(counter_create_typed) != ITT_VERSIONIZE(ITT_JOIN(_N_(counter_create_typed),_init)))
728         {
729             if (PTHREAD_SYMBOLS) __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
730             return ITTNOTIFY_NAME(counter_create_typed)(name, domain, type);
731         }
732 #endif
733         else
734         {
735 #if ITT_PLATFORM==ITT_PLATFORM_WIN
736             __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
737 #else
738             if (PTHREAD_SYMBOLS) __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
739 #endif
740             return NULL;
741         }
742     }
743     if (__itt_is_collector_available())
744     {
745         for (h_tail = NULL, h = _N_(_ittapi_global).counter_list; h != NULL; h_tail = h, h = h->next)
746         {
747             if (h->nameA != NULL && h->type == (int)type && !__itt_fstrcmp(h->nameA, name) && ((h->domainA == NULL && domain == NULL) ||
748                 (h->domainA != NULL && domain != NULL && !__itt_fstrcmp(h->domainA, domain)))) break;
749         }
750         if (h == NULL)
751         {
752             NEW_COUNTER_A(&_N_(_ittapi_global), h, h_tail, name, domain, type);
753         }
754     }
755     if (PTHREAD_SYMBOLS) __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
756     return (__itt_counter)h;
757 }
758 
759 #if ITT_PLATFORM==ITT_PLATFORM_WIN
760 static __itt_histogram* ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(histogram_createW),_init))(const __itt_domain* domain, const wchar_t* name, __itt_metadata_type x_type, __itt_metadata_type y_type)
761 {
762     __itt_histogram *h_tail = NULL, *h = NULL;
763 
764     if (domain == NULL || name == NULL)
765     {
766         return NULL;
767     }
768 
769     ITT_MUTEX_INIT_AND_LOCK(_N_(_ittapi_global));
770     if (_N_(_ittapi_global).api_initialized)
771     {
772         if (ITTNOTIFY_NAME(histogram_createW) && ITTNOTIFY_NAME(histogram_createW) != ITT_VERSIONIZE(ITT_JOIN(_N_(histogram_createW),_init)))
773         {
774             __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
775             return ITTNOTIFY_NAME(histogram_createW)(domain, name, x_type, y_type);
776         }
777         else
778         {
779             __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
780             return NULL;
781         }
782     }
783     if (__itt_is_collector_available())
784     {
785         for (h_tail = NULL, h = _N_(_ittapi_global).histogram_list; h != NULL; h_tail = h, h = h->next)
786         {
787             if (h->domain == NULL) continue;
788             else if (h->domain == domain && h->nameW != NULL && !wcscmp(h->nameW, name)) break;
789         }
790         if (h == NULL)
791         {
792             NEW_HISTOGRAM_W(&_N_(_ittapi_global), h, h_tail, domain, name, x_type, y_type);
793         }
794     }
795     __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
796     return (__itt_histogram*)h;
797 }
798 
799 static __itt_histogram* ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(histogram_createA),_init))(const __itt_domain* domain, const char* name, __itt_metadata_type x_type, __itt_metadata_type y_type)
800 #else  /* ITT_PLATFORM!=ITT_PLATFORM_WIN */
801 static __itt_histogram* ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(histogram_create),_init))(const __itt_domain* domain, const char* name, __itt_metadata_type x_type, __itt_metadata_type y_type)
802 #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
803 {
804     __itt_histogram *h_tail = NULL, *h = NULL;
805 
806     if (domain == NULL || name == NULL)
807     {
808         return NULL;
809     }
810 
811     ITT_MUTEX_INIT_AND_LOCK(_N_(_ittapi_global));
812     if (_N_(_ittapi_global).api_initialized)
813     {
814 #if ITT_PLATFORM==ITT_PLATFORM_WIN
815         if (ITTNOTIFY_NAME(histogram_createA) && ITTNOTIFY_NAME(histogram_createA) != ITT_VERSIONIZE(ITT_JOIN(_N_(histogram_createA),_init)))
816         {
817             __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
818             return ITTNOTIFY_NAME(histogram_createA)(domain, name, x_type, y_type);
819         }
820 #else
821         if (ITTNOTIFY_NAME(histogram_create) && ITTNOTIFY_NAME(histogram_create) != ITT_VERSIONIZE(ITT_JOIN(_N_(histogram_create),_init)))
822         {
823             if (PTHREAD_SYMBOLS) __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
824             return ITTNOTIFY_NAME(histogram_create)(domain, name, x_type, y_type);
825         }
826 #endif
827         else
828         {
829 #if ITT_PLATFORM==ITT_PLATFORM_WIN
830             __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
831 #else
832             if (PTHREAD_SYMBOLS) __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
833 #endif
834             return NULL;
835         }
836     }
837     if (__itt_is_collector_available())
838     {
839         for (h_tail = NULL, h = _N_(_ittapi_global).histogram_list; h != NULL; h_tail = h, h = h->next)
840         {
841             if (h->domain == NULL) continue;
842             else if (h->domain == domain && h->nameA != NULL && !__itt_fstrcmp(h->nameA, name)) break;
843         }
844         if (h == NULL)
845         {
846             NEW_HISTOGRAM_A(&_N_(_ittapi_global), h, h_tail, domain, name, x_type, y_type);
847         }
848     }
849     if (PTHREAD_SYMBOLS) __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
850     return (__itt_histogram*)h;
851 }
852 
853 /* -------------------------------------------------------------------------- */
854 
855 static void ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(pause),_init))(void)
856 {
857     if (!_N_(_ittapi_global).api_initialized && _N_(_ittapi_global).thread_list == NULL)
858     {
859         __itt_init_ittlib_name(NULL, __itt_group_all);
860     }
861     if (ITTNOTIFY_NAME(pause) && ITTNOTIFY_NAME(pause) != ITT_VERSIONIZE(ITT_JOIN(_N_(pause),_init)))
862     {
863         ITTNOTIFY_NAME(pause)();
864     }
865 }
866 
867 static void ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(resume),_init))(void)
868 {
869     if (!_N_(_ittapi_global).api_initialized && _N_(_ittapi_global).thread_list == NULL)
870     {
871         __itt_init_ittlib_name(NULL, __itt_group_all);
872     }
873     if (ITTNOTIFY_NAME(resume) && ITTNOTIFY_NAME(resume) != ITT_VERSIONIZE(ITT_JOIN(_N_(resume),_init)))
874     {
875         ITTNOTIFY_NAME(resume)();
876     }
877 }
878 
879 #if ITT_PLATFORM==ITT_PLATFORM_WIN
880 static void ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(thread_set_nameW),_init))(const wchar_t* name)
881 {
882     if (!_N_(_ittapi_global).api_initialized && _N_(_ittapi_global).thread_list == NULL)
883     {
884         __itt_init_ittlib_name(NULL, __itt_group_all);
885     }
886     if (ITTNOTIFY_NAME(thread_set_nameW) && ITTNOTIFY_NAME(thread_set_nameW) != ITT_VERSIONIZE(ITT_JOIN(_N_(thread_set_nameW),_init)))
887     {
888         ITTNOTIFY_NAME(thread_set_nameW)(name);
889     }
890 }
891 
892 static int ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(thr_name_setW),_init))(const wchar_t* name, int namelen)
893 {
894     (void)namelen;
895     ITT_VERSIONIZE(ITT_JOIN(_N_(thread_set_nameW),_init))(name);
896     return 0;
897 }
898 
899 static void ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(thread_set_nameA),_init))(const char* name)
900 #else  /* ITT_PLATFORM==ITT_PLATFORM_WIN */
901 static void ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(thread_set_name),_init))(const char* name)
902 #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
903 {
904     if (!_N_(_ittapi_global).api_initialized && _N_(_ittapi_global).thread_list == NULL)
905     {
906         __itt_init_ittlib_name(NULL, __itt_group_all);
907     }
908 #if ITT_PLATFORM==ITT_PLATFORM_WIN
909     if (ITTNOTIFY_NAME(thread_set_nameA) && ITTNOTIFY_NAME(thread_set_nameA) != ITT_VERSIONIZE(ITT_JOIN(_N_(thread_set_nameA),_init)))
910     {
911         ITTNOTIFY_NAME(thread_set_nameA)(name);
912     }
913 #else  /* ITT_PLATFORM==ITT_PLATFORM_WIN */
914     if (ITTNOTIFY_NAME(thread_set_name) && ITTNOTIFY_NAME(thread_set_name) != ITT_VERSIONIZE(ITT_JOIN(_N_(thread_set_name),_init)))
915     {
916         ITTNOTIFY_NAME(thread_set_name)(name);
917     }
918 #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
919 }
920 
921 #if ITT_PLATFORM==ITT_PLATFORM_WIN
922 static int ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(thr_name_setA),_init))(const char* name, int namelen)
923 {
924     (void)namelen;
925     ITT_VERSIONIZE(ITT_JOIN(_N_(thread_set_nameA),_init))(name);
926     return 0;
927 }
928 #else  /* ITT_PLATFORM==ITT_PLATFORM_WIN */
929 static int ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(thr_name_set),_init))(const char* name, int namelen)
930 {
931     (void)namelen;
932     ITT_VERSIONIZE(ITT_JOIN(_N_(thread_set_name),_init))(name);
933     return 0;
934 }
935 #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
936 
937 static void ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(thread_ignore),_init))(void)
938 {
939     if (!_N_(_ittapi_global).api_initialized && _N_(_ittapi_global).thread_list == NULL)
940     {
941         __itt_init_ittlib_name(NULL, __itt_group_all);
942     }
943     if (ITTNOTIFY_NAME(thread_ignore) && ITTNOTIFY_NAME(thread_ignore) != ITT_VERSIONIZE(ITT_JOIN(_N_(thread_ignore),_init)))
944     {
945         ITTNOTIFY_NAME(thread_ignore)();
946     }
947 }
948 
949 static void ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(thr_ignore),_init))(void)
950 {
951     ITT_VERSIONIZE(ITT_JOIN(_N_(thread_ignore),_init))();
952 }
953 
954 static void ITTAPI ITT_VERSIONIZE(ITT_JOIN(_N_(enable_attach),_init))(void)
955 {
956 #ifdef __ANDROID__
957     /*
958      * if LIB_VAR_NAME env variable were set before then stay previous value
959      * else set default path
960     */
961     setenv(ITT_TO_STR(LIB_VAR_NAME), ANDROID_ITTNOTIFY_DEFAULT_PATH, 0);
962 #endif
963 }
964 
965 /* -------------------------------------------------------------------------- */
966 
967 static const char* __itt_fsplit(const char* s, const char* sep, const char** out, int* len)
968 {
969     int i;
970     int j;
971 
972     if (!s || !sep || !out || !len)
973         return NULL;
974 
975     for (i = 0; s[i]; i++)
976     {
977         int b = 0;
978         for (j = 0; sep[j]; j++)
979             if (s[i] == sep[j])
980             {
981                 b = 1;
982                 break;
983             }
984         if (!b)
985             break;
986     }
987 
988     if (!s[i])
989         return NULL;
990 
991     *len = 0;
992     *out = &s[i];
993 
994     for (; s[i]; i++, (*len)++)
995     {
996         int b = 0;
997         for (j = 0; sep[j]; j++)
998             if (s[i] == sep[j])
999             {
1000                 b = 1;
1001                 break;
1002             }
1003         if (b)
1004             break;
1005     }
1006 
1007     for (; s[i]; i++)
1008     {
1009         int b = 0;
1010         for (j = 0; sep[j]; j++)
1011             if (s[i] == sep[j])
1012             {
1013                 b = 1;
1014                 break;
1015             }
1016         if (!b)
1017             break;
1018     }
1019 
1020     return &s[i];
1021 }
1022 
1023 /* This function return value of env variable that placed into static buffer.
1024  * !!! The same static buffer is used for subsequent calls. !!!
1025  * This was done to avoid dynamic allocation for few calls.
1026  * Actually we need this function only four times.
1027  */
1028 static const char* __itt_get_env_var(const char* name)
1029 {
1030 #define MAX_ENV_VALUE_SIZE 4086
1031     static char  env_buff[MAX_ENV_VALUE_SIZE];
1032     static char* env_value = (char*)env_buff;
1033 
1034     if (name != NULL)
1035     {
1036 #if ITT_PLATFORM==ITT_PLATFORM_WIN
1037         size_t max_len = MAX_ENV_VALUE_SIZE - (size_t)(env_value - env_buff);
1038         DWORD rc = GetEnvironmentVariableA(name, env_value, (DWORD)max_len);
1039         if (rc >= max_len)
1040             __itt_report_error(__itt_error_env_too_long, name, (size_t)rc - 1, (size_t)(max_len - 1));
1041         else if (rc > 0)
1042         {
1043             const char* ret = (const char*)env_value;
1044             env_value += rc + 1;
1045             return ret;
1046         }
1047         else
1048         {
1049             /* If environment variable is empty, GetEnvironmentVariables()
1050              * returns zero (number of characters (not including terminating null),
1051              * and GetLastError() returns ERROR_SUCCESS. */
1052             DWORD err = GetLastError();
1053             if (err == ERROR_SUCCESS)
1054                 return env_value;
1055 
1056             if (err != ERROR_ENVVAR_NOT_FOUND)
1057                 __itt_report_error(__itt_error_cant_read_env, name, (int)err);
1058         }
1059 #else  /* ITT_PLATFORM!=ITT_PLATFORM_WIN */
1060         char* env = getenv(name);
1061         if (env != NULL)
1062         {
1063             size_t len = __itt_fstrnlen(env, MAX_ENV_VALUE_SIZE);
1064             size_t max_len = MAX_ENV_VALUE_SIZE - (size_t)(env_value - env_buff);
1065             if (len < max_len)
1066             {
1067                 const char* ret = (const char*)env_value;
1068                 __itt_fstrcpyn(env_value, max_len, env, len + 1);
1069                 env_value += len + 1;
1070                 return ret;
1071             } else
1072                 __itt_report_error(__itt_error_env_too_long, name, (size_t)len, (size_t)(max_len - 1));
1073         }
1074 #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
1075     }
1076     return NULL;
1077 }
1078 
1079 static const char* __itt_get_lib_name(void)
1080 {
1081     const char* lib_name = __itt_get_env_var(ITT_TO_STR(LIB_VAR_NAME));
1082 
1083 #ifdef __ANDROID__
1084     if (lib_name == NULL)
1085     {
1086 
1087 #if ITT_ARCH==ITT_ARCH_IA32 || ITT_ARCH==ITT_ARCH_ARM
1088         const char* const marker_filename = "com.intel.itt.collector_lib_32";
1089 #else
1090         const char* const marker_filename = "com.intel.itt.collector_lib_64";
1091 #endif
1092 
1093         char system_wide_marker_filename[PATH_MAX] = {0};
1094         int itt_marker_file_fd = -1;
1095         ssize_t res = 0;
1096 
1097         res = snprintf(system_wide_marker_filename, PATH_MAX - 1, "%s%s", "/data/local/tmp/", marker_filename);
1098         if (res < 0)
1099         {
1100             ITT_ANDROID_LOGE("Unable to concatenate marker file string.");
1101             return lib_name;
1102         }
1103         itt_marker_file_fd = open(system_wide_marker_filename, O_RDONLY);
1104 
1105         if (itt_marker_file_fd == -1)
1106         {
1107             const pid_t my_pid = getpid();
1108             char cmdline_path[PATH_MAX] = {0};
1109             char package_name[PATH_MAX] = {0};
1110             char app_sandbox_file[PATH_MAX] = {0};
1111             int cmdline_fd = 0;
1112 
1113             ITT_ANDROID_LOGI("Unable to open system-wide marker file.");
1114             res = snprintf(cmdline_path, PATH_MAX - 1, "/proc/%d/cmdline", my_pid);
1115             if (res < 0)
1116             {
1117                 ITT_ANDROID_LOGE("Unable to get cmdline path string.");
1118                 return lib_name;
1119             }
1120 
1121             ITT_ANDROID_LOGI("CMD file: %s\n", cmdline_path);
1122             cmdline_fd = open(cmdline_path, O_RDONLY);
1123             if (cmdline_fd == -1)
1124             {
1125                 ITT_ANDROID_LOGE("Unable to open %s file!", cmdline_path);
1126                 return lib_name;
1127             }
1128             res = read(cmdline_fd, package_name, PATH_MAX - 1);
1129             if (res == -1)
1130             {
1131                 ITT_ANDROID_LOGE("Unable to read %s file!", cmdline_path);
1132                 res = close(cmdline_fd);
1133                 if (res == -1)
1134                 {
1135                     ITT_ANDROID_LOGE("Unable to close %s file!", cmdline_path);
1136                 }
1137                 return lib_name;
1138             }
1139             res = close(cmdline_fd);
1140             if (res == -1)
1141             {
1142                 ITT_ANDROID_LOGE("Unable to close %s file!", cmdline_path);
1143                 return lib_name;
1144             }
1145             ITT_ANDROID_LOGI("Package name: %s\n", package_name);
1146             res = snprintf(app_sandbox_file, PATH_MAX - 1, "/data/data/%s/%s", package_name, marker_filename);
1147             if (res < 0)
1148             {
1149                 ITT_ANDROID_LOGE("Unable to concatenate marker file string.");
1150                 return lib_name;
1151             }
1152 
1153             ITT_ANDROID_LOGI("Lib marker file name: %s\n", app_sandbox_file);
1154             itt_marker_file_fd = open(app_sandbox_file, O_RDONLY);
1155             if (itt_marker_file_fd == -1)
1156             {
1157                 ITT_ANDROID_LOGE("Unable to open app marker file!");
1158                 return lib_name;
1159             }
1160         }
1161 
1162         {
1163             char itt_lib_name[PATH_MAX] = {0};
1164 
1165             res = read(itt_marker_file_fd, itt_lib_name, PATH_MAX - 1);
1166             if (res == -1)
1167             {
1168                 ITT_ANDROID_LOGE("Unable to read %s file!", itt_marker_file_fd);
1169                 res = close(itt_marker_file_fd);
1170                 if (res == -1)
1171                 {
1172                     ITT_ANDROID_LOGE("Unable to close %s file!", itt_marker_file_fd);
1173                 }
1174                 return lib_name;
1175             }
1176             ITT_ANDROID_LOGI("ITT Lib path: %s", itt_lib_name);
1177             res = close(itt_marker_file_fd);
1178             if (res == -1)
1179             {
1180                 ITT_ANDROID_LOGE("Unable to close %s file!", itt_marker_file_fd);
1181                 return lib_name;
1182             }
1183             ITT_ANDROID_LOGI("Set env %s to %s", ITT_TO_STR(LIB_VAR_NAME), itt_lib_name);
1184             res = setenv(ITT_TO_STR(LIB_VAR_NAME), itt_lib_name, 0);
1185             if (res == -1)
1186             {
1187                 ITT_ANDROID_LOGE("Unable to set env var!");
1188                 return lib_name;
1189             }
1190             lib_name = __itt_get_env_var(ITT_TO_STR(LIB_VAR_NAME));
1191             ITT_ANDROID_LOGI("ITT Lib path from env: %s", lib_name);
1192         }
1193     }
1194 #endif
1195 
1196     return lib_name;
1197 }
1198 
1199 /* Avoid clashes with std::min */
1200 #define __itt_min(a,b) ((a) < (b) ? (a) : (b))
1201 
1202 static __itt_group_id __itt_get_groups(void)
1203 {
1204     int i;
1205     __itt_group_id res = __itt_group_none;
1206     const char* var_name  = "INTEL_ITTNOTIFY_GROUPS";
1207     const char* group_str = __itt_get_env_var(var_name);
1208 
1209     if (group_str != NULL)
1210     {
1211         int len;
1212         char gr[255];
1213         const char* chunk;
1214         while ((group_str = __itt_fsplit(group_str, ",; ", &chunk, &len)) != NULL)
1215         {
1216             int min_len = __itt_min(len, (int)(sizeof(gr) - 1));
1217             __itt_fstrcpyn(gr, sizeof(gr) - 1, chunk,  min_len);
1218             gr[min_len] = 0;
1219 
1220             for (i = 0; group_list[i].name != NULL; i++)
1221             {
1222                 if (!__itt_fstrcmp(gr, group_list[i].name))
1223                 {
1224                     res = (__itt_group_id)(res | group_list[i].id);
1225                     break;
1226                 }
1227             }
1228         }
1229         /* TODO: !!! Workaround for bug with warning for unknown group !!!
1230          * Should be fixed in new initialization scheme.
1231          * Now the following groups should be set always. */
1232         for (i = 0; group_list[i].id != __itt_group_none; i++)
1233             if (group_list[i].id != __itt_group_all &&
1234                 group_list[i].id > __itt_group_splitter_min &&
1235                 group_list[i].id < __itt_group_splitter_max)
1236                 res = (__itt_group_id)(res | group_list[i].id);
1237         return res;
1238     }
1239     else
1240     {
1241         for (i = 0; group_alias[i].env_var != NULL; i++)
1242             if (__itt_get_env_var(group_alias[i].env_var) != NULL)
1243                 return group_alias[i].groups;
1244     }
1245 
1246     return res;
1247 }
1248 
1249 #undef __itt_min
1250 
1251 static int __itt_lib_version(lib_t lib)
1252 {
1253     if (lib == NULL)
1254         return 0;
1255     if (__itt_get_proc(lib, "__itt_api_init"))
1256         return 2;
1257     if (__itt_get_proc(lib, "__itt_api_version"))
1258         return 1;
1259     return 0;
1260 }
1261 
1262 /* It's not used right now! Comment it out to avoid warnings.
1263 static void __itt_reinit_all_pointers(void)
1264 {
1265     register int i;
1266     // Fill all pointers with initial stubs
1267     for (i = 0; _N_(_ittapi_global).api_list_ptr[i].name != NULL; i++)
1268         *_N_(_ittapi_global).api_list_ptr[i].func_ptr = _N_(_ittapi_global).api_list_ptr[i].init_func;
1269 }
1270 */
1271 
1272 static void __itt_nullify_all_pointers(void)
1273 {
1274     int i;
1275     /* Nulify all pointers except domain_create, string_handle_create  and counter_create */
1276     for (i = 0; _N_(_ittapi_global).api_list_ptr[i].name != NULL; i++)
1277         *_N_(_ittapi_global).api_list_ptr[i].func_ptr = _N_(_ittapi_global).api_list_ptr[i].null_func;
1278 }
1279 
1280 static int __itt_is_collector_available(void)
1281 {
1282     int is_available;
1283 
1284     ITT_MUTEX_INIT_AND_LOCK(_N_(_ittapi_global));
1285     if (_N_(_ittapi_global).state == __itt_collection_uninitialized)
1286     {
1287         _N_(_ittapi_global).state = (NULL == __itt_get_lib_name()) ? __itt_collection_collector_absent : __itt_collection_collector_exists;
1288     }
1289     is_available = (_N_(_ittapi_global).state == __itt_collection_collector_exists ||
1290         _N_(_ittapi_global).state == __itt_collection_init_successful);
1291     __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
1292     return is_available;
1293 }
1294 
1295 #if ITT_PLATFORM==ITT_PLATFORM_WIN
1296 #if _MSC_VER
1297 #pragma warning(push)
1298 #pragma warning(disable: 4054) /* warning C4054: 'type cast' : from function pointer 'XXX' to data pointer 'void *' */
1299 #pragma warning(disable: 4055) /* warning C4055: 'type cast' : from data pointer 'void *' to function pointer 'XXX' */
1300 #endif
1301 #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
1302 
1303 ITT_EXTERN_C void _N_(fini_ittlib)(void)
1304 {
1305     __itt_api_fini_t* __itt_api_fini_ptr = NULL;
1306     static volatile TIDT current_thread = 0;
1307 
1308     if (_N_(_ittapi_global).api_initialized)
1309     {
1310         ITT_MUTEX_INIT_AND_LOCK(_N_(_ittapi_global));
1311         if (_N_(_ittapi_global).api_initialized)
1312         {
1313             if (current_thread == 0)
1314             {
1315                 if (PTHREAD_SYMBOLS) current_thread = __itt_thread_id();
1316                 if (_N_(_ittapi_global).lib != NULL)
1317                 {
1318                     __itt_api_fini_ptr = (__itt_api_fini_t*)(size_t)__itt_get_proc(_N_(_ittapi_global).lib, "__itt_api_fini");
1319                 }
1320                 if (__itt_api_fini_ptr)
1321                 {
1322                     __itt_api_fini_ptr(&_N_(_ittapi_global));
1323                 }
1324 
1325                 __itt_nullify_all_pointers();
1326 
1327  /* TODO: !!! not safe !!! don't support unload so far.
1328   *             if (_N_(_ittapi_global).lib != NULL)
1329   *                 __itt_unload_lib(_N_(_ittapi_global).lib);
1330   *             _N_(_ittapi_global).lib = NULL;
1331   */
1332                 _N_(_ittapi_global).api_initialized = 0;
1333                 current_thread = 0;
1334             }
1335         }
1336         if (PTHREAD_SYMBOLS) __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
1337     }
1338 }
1339 
1340 /* !!! this function should be called under mutex lock !!! */
1341 static void __itt_free_allocated_resources(void)
1342 {
1343     __itt_string_handle* current_string = _N_(_ittapi_global).string_list;
1344     while (current_string != NULL)
1345     {
1346         __itt_string_handle* tmp = current_string->next;
1347         free((char*)current_string->strA);
1348 #if ITT_PLATFORM==ITT_PLATFORM_WIN
1349         free((wchar_t*)current_string->strW);
1350 #endif
1351         free(current_string);
1352         current_string = tmp;
1353     }
1354     _N_(_ittapi_global).string_list = NULL;
1355 
1356     __itt_domain* current_domain = _N_(_ittapi_global).domain_list;
1357     while (current_domain != NULL)
1358     {
1359         __itt_domain* tmp = current_domain->next;
1360         free((char*)current_domain->nameA);
1361 #if ITT_PLATFORM==ITT_PLATFORM_WIN
1362         free((wchar_t*)current_domain->nameW);
1363 #endif
1364         free(current_domain);
1365         current_domain = tmp;
1366     }
1367     _N_(_ittapi_global).domain_list = NULL;
1368 
1369     __itt_counter_info_t* current_couter = _N_(_ittapi_global).counter_list;
1370     while (current_couter != NULL)
1371     {
1372         __itt_counter_info_t* tmp = current_couter->next;
1373         free((char*)current_couter->nameA);
1374         free((char*)current_couter->domainA);
1375 #if ITT_PLATFORM==ITT_PLATFORM_WIN
1376         free((wchar_t*)current_couter->nameW);
1377         free((wchar_t*)current_couter->domainW);
1378 #endif
1379         free(current_couter);
1380         current_couter = tmp;
1381     }
1382     _N_(_ittapi_global).counter_list = NULL;
1383 
1384     __itt_histogram* current_histogram = _N_(_ittapi_global).histogram_list;
1385     while (current_histogram != NULL)
1386     {
1387         __itt_histogram* tmp = current_histogram->next;
1388         free((char*)current_histogram->nameA);
1389 #if ITT_PLATFORM==ITT_PLATFORM_WIN
1390         free((wchar_t*)current_histogram->nameW);
1391 #endif
1392         free(current_histogram);
1393         current_histogram = tmp;
1394     }
1395     _N_(_ittapi_global).histogram_list = NULL;
1396 }
1397 
1398 ITT_EXTERN_C int _N_(init_ittlib)(const char* lib_name, __itt_group_id init_groups)
1399 {
1400     int i;
1401     __itt_group_id groups;
1402 #ifdef ITT_COMPLETE_GROUP
1403     __itt_group_id zero_group = __itt_group_none;
1404 #endif /* ITT_COMPLETE_GROUP */
1405     static volatile TIDT current_thread = 0;
1406 
1407     if (!_N_(_ittapi_global).api_initialized)
1408     {
1409 #ifndef ITT_SIMPLE_INIT
1410         ITT_MUTEX_INIT_AND_LOCK(_N_(_ittapi_global));
1411 #endif /* ITT_SIMPLE_INIT */
1412 
1413         if (!_N_(_ittapi_global).api_initialized)
1414         {
1415             if (current_thread == 0)
1416             {
1417                 if (PTHREAD_SYMBOLS) current_thread = __itt_thread_id();
1418                 if (lib_name == NULL)
1419                 {
1420                     lib_name = __itt_get_lib_name();
1421                 }
1422                 groups = __itt_get_groups();
1423                 if (DL_SYMBOLS && (groups != __itt_group_none || lib_name != NULL))
1424                 {
1425                     _N_(_ittapi_global).lib = __itt_load_lib((lib_name == NULL) ? ittnotify_lib_name : lib_name);
1426 
1427                     if (_N_(_ittapi_global).lib != NULL)
1428                     {
1429                         _N_(_ittapi_global).state = __itt_collection_init_successful;
1430                         __itt_api_init_t* __itt_api_init_ptr;
1431                         int lib_version = __itt_lib_version(_N_(_ittapi_global).lib);
1432 
1433                         switch (lib_version)
1434                         {
1435                         case 0:
1436                             groups = __itt_group_legacy;
1437                             ITT_ATTRIBUTE_FALLTHROUGH;
1438                         case 1:
1439                             /* Fill all pointers from dynamic library */
1440                             for (i = 0; _N_(_ittapi_global).api_list_ptr[i].name != NULL; i++)
1441                             {
1442                                 if (_N_(_ittapi_global).api_list_ptr[i].group & groups & init_groups)
1443                                 {
1444                                     *_N_(_ittapi_global).api_list_ptr[i].func_ptr = (void*)__itt_get_proc(_N_(_ittapi_global).lib, _N_(_ittapi_global).api_list_ptr[i].name);
1445                                     if (*_N_(_ittapi_global).api_list_ptr[i].func_ptr == NULL)
1446                                     {
1447                                         /* Restore pointers for function with static implementation */
1448                                         *_N_(_ittapi_global).api_list_ptr[i].func_ptr = _N_(_ittapi_global).api_list_ptr[i].null_func;
1449                                         __itt_report_error(__itt_error_no_symbol, lib_name, _N_(_ittapi_global).api_list_ptr[i].name);
1450 #ifdef ITT_COMPLETE_GROUP
1451                                         zero_group = (__itt_group_id)(zero_group | _N_(_ittapi_global).api_list_ptr[i].group);
1452 #endif /* ITT_COMPLETE_GROUP */
1453                                     }
1454                                 }
1455                                 else
1456                                     *_N_(_ittapi_global).api_list_ptr[i].func_ptr = _N_(_ittapi_global).api_list_ptr[i].null_func;
1457                             }
1458 
1459                             if (groups == __itt_group_legacy)
1460                             {
1461                                 /* Compatibility with legacy tools */
1462                                 ITTNOTIFY_NAME(thread_ignore)  = ITTNOTIFY_NAME(thr_ignore);
1463 #if ITT_PLATFORM==ITT_PLATFORM_WIN
1464                                 ITTNOTIFY_NAME(sync_createA)   = ITTNOTIFY_NAME(sync_set_nameA);
1465                                 ITTNOTIFY_NAME(sync_createW)   = ITTNOTIFY_NAME(sync_set_nameW);
1466 #else  /* ITT_PLATFORM!=ITT_PLATFORM_WIN */
1467                                 ITTNOTIFY_NAME(sync_create)    = ITTNOTIFY_NAME(sync_set_name);
1468 #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
1469                                 ITTNOTIFY_NAME(sync_prepare)   = ITTNOTIFY_NAME(notify_sync_prepare);
1470                                 ITTNOTIFY_NAME(sync_cancel)    = ITTNOTIFY_NAME(notify_sync_cancel);
1471                                 ITTNOTIFY_NAME(sync_acquired)  = ITTNOTIFY_NAME(notify_sync_acquired);
1472                                 ITTNOTIFY_NAME(sync_releasing) = ITTNOTIFY_NAME(notify_sync_releasing);
1473                             }
1474 
1475 #ifdef ITT_COMPLETE_GROUP
1476                             for (i = 0; _N_(_ittapi_global).api_list_ptr[i].name != NULL; i++)
1477                                 if (_N_(_ittapi_global).api_list_ptr[i].group & zero_group)
1478                                     *_N_(_ittapi_global).api_list_ptr[i].func_ptr = _N_(_ittapi_global).api_list_ptr[i].null_func;
1479 #endif /* ITT_COMPLETE_GROUP */
1480                             break;
1481                         case 2:
1482                             __itt_api_init_ptr = (__itt_api_init_t*)(size_t)__itt_get_proc(_N_(_ittapi_global).lib, "__itt_api_init");
1483                             if (__itt_api_init_ptr)
1484                                 __itt_api_init_ptr(&_N_(_ittapi_global), init_groups);
1485                             break;
1486                         }
1487                     }
1488                     else
1489                     {
1490                         _N_(_ittapi_global).state = __itt_collection_init_fail;
1491                         __itt_free_allocated_resources();
1492                         __itt_nullify_all_pointers();
1493 
1494                         __itt_report_error(__itt_error_no_module, lib_name,
1495 #if ITT_PLATFORM==ITT_PLATFORM_WIN
1496                             __itt_system_error()
1497 #else  /* ITT_PLATFORM==ITT_PLATFORM_WIN */
1498                             dlerror()
1499 #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
1500                         );
1501                     }
1502                 }
1503                 else
1504                 {
1505                     _N_(_ittapi_global).state = __itt_collection_collector_absent;
1506                     __itt_nullify_all_pointers();
1507                 }
1508                 _N_(_ittapi_global).api_initialized = 1;
1509                 current_thread = 0;
1510                 /* !!! Just to avoid unused code elimination !!! */
1511                 if (__itt_fini_ittlib_ptr == _N_(fini_ittlib)) current_thread = 0;
1512             }
1513         }
1514 
1515 #ifndef ITT_SIMPLE_INIT
1516         if (PTHREAD_SYMBOLS) __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
1517 #endif /* ITT_SIMPLE_INIT */
1518     }
1519 
1520     /* Evaluating if any function ptr is non empty and it's in init_groups */
1521     for (i = 0; _N_(_ittapi_global).api_list_ptr[i].name != NULL; i++)
1522     {
1523         if (*_N_(_ittapi_global).api_list_ptr[i].func_ptr != _N_(_ittapi_global).api_list_ptr[i].null_func &&
1524             _N_(_ittapi_global).api_list_ptr[i].group & init_groups)
1525         {
1526             return 1;
1527         }
1528     }
1529     return 0;
1530 }
1531 
1532 ITT_EXTERN_C __itt_error_handler_t* _N_(set_error_handler)(__itt_error_handler_t* handler)
1533 {
1534     __itt_error_handler_t* prev = (__itt_error_handler_t*)(size_t)_N_(_ittapi_global).error_handler;
1535     _N_(_ittapi_global).error_handler = (void*)(size_t)handler;
1536     return prev;
1537 }
1538 
1539 #if ITT_PLATFORM==ITT_PLATFORM_WIN
1540 #if _MSC_VER
1541 #pragma warning(pop)
1542 #endif
1543 #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
1544 
1545 /** __itt_mark_pt_region functions marks region of interest
1546  * region parameter defines different regions.
1547  * 0 <= region < 8 */
1548 
1549 #if defined(ITT_API_IPT_SUPPORT) && (ITT_PLATFORM==ITT_PLATFORM_WIN || ITT_PLATFORM==ITT_PLATFORM_POSIX) && !defined(__ANDROID__)
1550 void __itt_pt_mark(__itt_pt_region region);
1551 void __itt_pt_mark_event(__itt_pt_region region);
1552 #endif
1553 
1554 ITT_EXTERN_C void _N_(mark_pt_region_begin)(__itt_pt_region region)
1555 {
1556 #if defined(ITT_API_IPT_SUPPORT) && (ITT_PLATFORM==ITT_PLATFORM_WIN || ITT_PLATFORM==ITT_PLATFORM_POSIX) && !defined(__ANDROID__)
1557     if (_N_(_ittapi_global).ipt_collect_events == 1)
1558     {
1559         __itt_pt_mark_event(2*region);
1560     }
1561     else
1562     {
1563         __itt_pt_mark(2*region);
1564     }
1565 #else
1566     (void)region;
1567 #endif
1568 }
1569 
1570 ITT_EXTERN_C void _N_(mark_pt_region_end)(__itt_pt_region region)
1571 {
1572 #if defined(ITT_API_IPT_SUPPORT) && (ITT_PLATFORM==ITT_PLATFORM_WIN || ITT_PLATFORM==ITT_PLATFORM_POSIX) && !defined(__ANDROID__)
1573     if (_N_(_ittapi_global).ipt_collect_events == 1)
1574     {
1575         __itt_pt_mark_event(2*region + 1);
1576     }
1577     else
1578     {
1579         __itt_pt_mark(2*region + 1);
1580     }
1581 #else
1582      (void)region;
1583 #endif
1584 }
1585 
1586 ITT_EXTERN_C __itt_collection_state (_N_(get_collection_state))(void)
1587 {
1588     if (!_N_(_ittapi_global).api_initialized && _N_(_ittapi_global).thread_list == NULL)
1589     {
1590         __itt_init_ittlib_name(NULL, __itt_group_all);
1591     }
1592     return _N_(_ittapi_global).state;
1593 }
1594 
1595 /* !!! should be called from the library destructor !!!
1596  * this function destroys the mutex and frees resources
1597  * allocated by ITT API static part
1598  */
1599 ITT_EXTERN_C void (_N_(release_resources))(void)
1600 {
1601     ITT_MUTEX_INIT_AND_LOCK(_N_(_ittapi_global));
1602     __itt_free_allocated_resources();
1603     if (PTHREAD_SYMBOLS) __itt_mutex_unlock(&_N_(_ittapi_global).mutex);
1604     ITT_MUTEX_DESTROY(_N_(_ittapi_global));
1605 }
1606