File indexing completed on 2026-05-10 08:45:12
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021 #ifndef NVTX_EXT_INIT_GUARD
0022 #error Never include this file directly -- it is automatically included by nvToolsExt.h (except when NVTX_NO_IMPL is defined).
0023 #endif
0024
0025 #if defined(NVTX_AS_SYSTEM_HEADER)
0026 #if defined(__clang__)
0027 #pragma clang system_header
0028 #elif defined(__GNUC__) || defined(__NVCOMPILER)
0029 #pragma GCC system_header
0030 #elif defined(_MSC_VER)
0031 #pragma system_header
0032 #endif
0033 #endif
0034
0035 #ifdef __cplusplus
0036 extern "C" {
0037 #endif
0038
0039
0040
0041
0042
0043 #if defined(_WIN32)
0044 #define NVTX_ATOMIC_WRITE_PTR(address, value) \
0045 InterlockedExchangePointer(NVTX_REINTERPRET_CAST(volatile PVOID*, (address)), \
0046 NVTX_REINTERPRET_CAST(PVOID, (value)))
0047 #define NVTX_ATOMIC_CAS_PTR(old, address, exchange, comparand) \
0048 (old) = NVTX_REINTERPRET_CAST(intptr_t, InterlockedCompareExchangePointer( \
0049 NVTX_REINTERPRET_CAST(volatile PVOID*, (address)), \
0050 NVTX_REINTERPRET_CAST(PVOID, (exchange)), \
0051 NVTX_REINTERPRET_CAST(PVOID, (comparand))))
0052 #elif defined(__GNUC__)
0053
0054 #define NVTX_ATOMIC_WRITE_PTR(address, value) \
0055 __sync_synchronize(); *address = value; __sync_synchronize()
0056 #define NVTX_ATOMIC_CAS_PTR(old, address, exchange, comparand) \
0057 old = __sync_val_compare_and_swap(address, comparand, exchange)
0058 #else
0059 #error The library does not support your configuration!
0060 #endif
0061
0062 #ifndef NVTX_SUPPORT_ALREADY_INJECTED_LIBRARY
0063
0064 #if defined(_WIN32)
0065
0066 #define NVTX_SUPPORT_ALREADY_INJECTED_LIBRARY 0
0067 #else
0068
0069
0070
0071 #define NVTX_SUPPORT_ALREADY_INJECTED_LIBRARY 0
0072 #endif
0073 #endif
0074
0075 #ifndef NVTX_SUPPORT_ENV_VARS
0076
0077
0078
0079 #define NVTX_SUPPORT_ENV_VARS 1
0080 #endif
0081
0082 #ifndef NVTX_SUPPORT_DYNAMIC_INJECTION_LIBRARY
0083
0084 #define NVTX_SUPPORT_DYNAMIC_INJECTION_LIBRARY 1
0085 #endif
0086
0087 #ifndef NVTX_SUPPORT_ANDROID_INJECTION_LIBRARY_IN_PACKAGE
0088 #if defined(__ANDROID__)
0089 #define NVTX_SUPPORT_ANDROID_INJECTION_LIBRARY_IN_PACKAGE 1
0090 #else
0091 #define NVTX_SUPPORT_ANDROID_INJECTION_LIBRARY_IN_PACKAGE 0
0092 #endif
0093 #endif
0094
0095 #ifndef NVTX_SUPPORT_STATIC_INJECTION_LIBRARY
0096
0097
0098
0099
0100
0101
0102
0103 #if defined(__GNUC__) && !defined(_WIN32) && !defined(__CYGWIN__)
0104 #define NVTX_SUPPORT_STATIC_INJECTION_LIBRARY 1
0105 #else
0106 #define NVTX_SUPPORT_STATIC_INJECTION_LIBRARY 0
0107 #endif
0108 #endif
0109
0110 #if NVTX_SUPPORT_STATIC_INJECTION_LIBRARY && !defined(NVTX_STATIC_INJECTION_IMPL)
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131 __attribute__((weak)) NvtxExtInitializeInjectionFunc_t InitializeInjectionNvtxExtension_fnptr;
0132 #endif
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150 NVTX_LINKONCE_FWDDECL_FUNCTION int NVTX_VERSIONED_IDENTIFIER(nvtxExtLoadInjectionLibrary)(
0151 NvtxExtInitializeInjectionFunc_t* out_init_fnptr);
0152 NVTX_LINKONCE_DEFINE_FUNCTION int NVTX_VERSIONED_IDENTIFIER(nvtxExtLoadInjectionLibrary)(
0153 NvtxExtInitializeInjectionFunc_t* out_init_fnptr)
0154 {
0155 static const char initFuncName[] = "InitializeInjectionNvtxExtension";
0156 #if NVTX_SUPPORT_ALREADY_INJECTED_LIBRARY
0157 static const char initFuncPreinjectName[] = "InitializeInjectionNvtxExtensionPreinject";
0158 #endif
0159 NvtxExtInitializeInjectionFunc_t init_fnptr = NVTX_NULLPTR;
0160 NVTX_DLLHANDLE injectionLibraryHandle = NVTX_DLLDEFAULT;
0161
0162 if (out_init_fnptr)
0163 {
0164 *out_init_fnptr = NVTX_NULLPTR;
0165 }
0166
0167 #if NVTX_SUPPORT_DYNAMIC_INJECTION_LIBRARY
0168
0169 {
0170 #if NVTX_SUPPORT_ENV_VARS
0171
0172
0173 const NVTX_PATHCHAR* const nvtxEnvVarName = (sizeof(void*) == 4)
0174 ? NVTX_STR("NVTX_INJECTION32_PATH")
0175 : NVTX_STR("NVTX_INJECTION64_PATH");
0176 #endif
0177 NVTX_PATHCHAR injectionLibraryPathBuf[NVTX_BUFSIZE];
0178 const NVTX_PATHCHAR* injectionLibraryPath = NVTX_NULLPTR;
0179
0180
0181 (void)injectionLibraryPathBuf;
0182
0183 #if NVTX_SUPPORT_ENV_VARS
0184
0185
0186 #if defined(_MSC_VER)
0187 #pragma warning( push )
0188 #pragma warning( disable : 4996 )
0189 #endif
0190 injectionLibraryPath = NVTX_GETENV(nvtxEnvVarName);
0191 #if defined(_MSC_VER)
0192 #pragma warning( pop )
0193 #endif
0194 #endif
0195
0196 #if NVTX_SUPPORT_ANDROID_INJECTION_LIBRARY_IN_PACKAGE
0197 if (!injectionLibraryPath)
0198 {
0199 const char *bits = (sizeof(void*) == 4) ? "32" : "64";
0200 char cmdlineBuf[32];
0201 char pkgName[PATH_MAX];
0202 int count;
0203 int pid;
0204 FILE *fp;
0205 size_t bytesRead;
0206 size_t pos;
0207
0208 pid = NVTX_STATIC_CAST(int, getpid());
0209 count = snprintf(cmdlineBuf, sizeof(cmdlineBuf), "/proc/%d/cmdline", pid);
0210 if (count <= 0 || count >= NVTX_STATIC_CAST(int, sizeof(cmdlineBuf)))
0211 {
0212 NVTX_ERR("Path buffer too small for: /proc/%d/cmdline\n", pid);
0213 return NVTX_ERR_INIT_ACCESS_LIBRARY;
0214 }
0215
0216 fp = fopen(cmdlineBuf, "r");
0217 if (!fp)
0218 {
0219 NVTX_ERR("File couldn't be opened: %s\n", cmdlineBuf);
0220 return NVTX_ERR_INIT_ACCESS_LIBRARY;
0221 }
0222
0223 bytesRead = fread(pkgName, 1, sizeof(pkgName) - 1, fp);
0224 fclose(fp);
0225 if (bytesRead == 0)
0226 {
0227 NVTX_ERR("Package name couldn't be read from file: %s\n", cmdlineBuf);
0228 return NVTX_ERR_INIT_ACCESS_LIBRARY;
0229 }
0230
0231 pkgName[bytesRead] = 0;
0232
0233
0234
0235 pos = 0;
0236 while (pos < bytesRead && pkgName[pos] != ':' && pkgName[pos] != '\0')
0237 {
0238 ++pos;
0239 }
0240 pkgName[pos] = 0;
0241
0242 count = snprintf(injectionLibraryPathBuf, NVTX_BUFSIZE, "/data/data/%s/files/libNvtxInjection%s.so", pkgName, bits);
0243 if (count <= 0 || count >= NVTX_BUFSIZE)
0244 {
0245 NVTX_ERR("Path buffer too small for: /data/data/%s/files/libNvtxInjection%s.so\n", pkgName, bits);
0246 return NVTX_ERR_INIT_ACCESS_LIBRARY;
0247 }
0248
0249
0250
0251
0252 if (injectionLibraryPathBuf[0] == '/')
0253 {
0254 #if (__ANDROID_API__ < 21)
0255 int access_err = access(injectionLibraryPathBuf, F_OK | R_OK);
0256 #else
0257 int access_err = faccessat(AT_FDCWD, injectionLibraryPathBuf, F_OK | R_OK, 0);
0258 #endif
0259 if (access_err != 0)
0260 {
0261 NVTX_ERR("Injection library path wasn't accessible [code=%s] [path=%s]\n", strerror(errno), injectionLibraryPathBuf);
0262 return NVTX_ERR_INIT_ACCESS_LIBRARY;
0263 }
0264 }
0265 injectionLibraryPath = injectionLibraryPathBuf;
0266 }
0267 #endif
0268
0269
0270
0271 if (injectionLibraryPath)
0272 {
0273
0274 injectionLibraryHandle = NVTX_DLLOPEN(injectionLibraryPath);
0275 if (!injectionLibraryHandle)
0276 {
0277 NVTX_ERR("Failed to load injection library\n");
0278 return NVTX_ERR_INIT_LOAD_LIBRARY;
0279 }
0280 else
0281 {
0282
0283 init_fnptr = NVTX_REINTERPRET_CAST(NvtxExtInitializeInjectionFunc_t, NVTX_DLLFUNC(injectionLibraryHandle, initFuncName));
0284 if (!init_fnptr)
0285 {
0286 NVTX_DLLCLOSE(injectionLibraryHandle);
0287 NVTX_ERR("Failed to get address of function %s from injection library\n", initFuncName);
0288 return NVTX_ERR_INIT_MISSING_LIBRARY_ENTRY_POINT;
0289 }
0290 }
0291 }
0292 }
0293 #endif
0294
0295 #if NVTX_SUPPORT_ALREADY_INJECTED_LIBRARY
0296 if (!init_fnptr)
0297 {
0298
0299 init_fnptr = NVTX_REINTERPRET_CAST(NvtxExtInitializeInjectionFunc_t, NVTX_DLLFUNC(NVTX_DLLDEFAULT, initFuncPreinjectName));
0300 }
0301 #endif
0302
0303 #if NVTX_SUPPORT_STATIC_INJECTION_LIBRARY
0304 if (!init_fnptr)
0305 {
0306
0307
0308
0309
0310 if (InitializeInjectionNvtxExtension_fnptr)
0311 {
0312 init_fnptr = InitializeInjectionNvtxExtension_fnptr;
0313 }
0314 }
0315 #endif
0316
0317 if (out_init_fnptr)
0318 {
0319 *out_init_fnptr = init_fnptr;
0320 }
0321
0322
0323
0324 if (!init_fnptr)
0325 {
0326 return NVTX_ERR_NO_INJECTION_LIBRARY_AVAILABLE;
0327 }
0328
0329 return NVTX_SUCCESS;
0330 }
0331
0332
0333 NVTX_LINKONCE_FWDDECL_FUNCTION void NVTX_VERSIONED_IDENTIFIER(nvtxExtInitOnce) (
0334 nvtxExtModuleInfo_t* moduleInfo, intptr_t* moduleState);
0335 NVTX_LINKONCE_DEFINE_FUNCTION void NVTX_VERSIONED_IDENTIFIER(nvtxExtInitOnce) (
0336 nvtxExtModuleInfo_t* moduleInfo, intptr_t* moduleState)
0337 {
0338 intptr_t old;
0339
0340 NVTX_INFO( "%s\n", __FUNCTION__ );
0341
0342 if (*moduleState == NVTX_EXTENSION_LOADED ||
0343 *moduleState == NVTX_EXTENSION_DISABLED ||
0344 *moduleState == NVTX_EXTENSION_INIT_FN_FAILED)
0345 {
0346 NVTX_INFO("Module loaded\n");
0347 return;
0348 }
0349
0350 NVTX_ATOMIC_CAS_PTR(
0351 old,
0352 moduleState,
0353 NVTX_EXTENSION_STARTING,
0354 NVTX_EXTENSION_FRESH);
0355 if (old == NVTX_EXTENSION_FRESH)
0356 {
0357 intptr_t stateReturnValue = NVTX_EXTENSION_LOADED;
0358 NvtxExtInitializeInjectionFunc_t init_fnptr =
0359 NVTX_VERSIONED_IDENTIFIER(nvtxExtGlobals1).injectionFnPtr;
0360 int entryPointStatus = 0;
0361 int forceAllToNoops = 0;
0362 size_t s;
0363
0364
0365 if (init_fnptr == NVTX_NULLPTR)
0366 {
0367 int result = 0;
0368
0369
0370 nvtxInitialize(NVTX_NULLPTR);
0371
0372 result = NVTX_VERSIONED_IDENTIFIER(nvtxExtLoadInjectionLibrary)(&init_fnptr);
0373
0374
0375 if (result == NVTX_SUCCESS)
0376 {
0377 NVTX_VERSIONED_IDENTIFIER(nvtxExtGlobals1).injectionFnPtr = init_fnptr;
0378 }
0379 else
0380 {
0381 if (result == NVTX_ERR_INIT_MISSING_LIBRARY_ENTRY_POINT)
0382 {
0383 stateReturnValue = NVTX_EXTENSION_DISABLED;
0384 }
0385 NVTX_ERR("Failed to load injection library.\n");
0386 }
0387 }
0388
0389 if (init_fnptr != NVTX_NULLPTR)
0390 {
0391
0392
0393 entryPointStatus = init_fnptr(moduleInfo);
0394 if (entryPointStatus == 0)
0395 {
0396 stateReturnValue = NVTX_EXTENSION_INIT_FN_FAILED;
0397 NVTX_ERR("Failed to initialize injection library -- initialization function returned 0\n");
0398 }
0399 }
0400
0401
0402
0403 forceAllToNoops = (init_fnptr == NVTX_NULLPTR) || (entryPointStatus == 0);
0404 for (s = 0; s < moduleInfo->segmentsCount; ++s)
0405 {
0406 nvtxExtModuleSegment_t* segment = moduleInfo->segments + s;
0407 size_t i;
0408 for (i = 0; i < segment->slotCount; ++i)
0409 {
0410 if (forceAllToNoops || (segment->functionSlots[i] == NVTX_EXTENSION_FRESH))
0411 {
0412 segment->functionSlots[i] = NVTX_EXTENSION_DISABLED;
0413 }
0414 }
0415 }
0416
0417 NVTX_MEMBAR();
0418
0419
0420 NVTX_ATOMIC_WRITE_PTR(moduleState, stateReturnValue);
0421 }
0422 else
0423 {
0424 NVTX_MEMBAR();
0425 while (*moduleState != NVTX_EXTENSION_LOADED &&
0426 *moduleState != NVTX_EXTENSION_DISABLED &&
0427 *moduleState != NVTX_EXTENSION_INIT_FN_FAILED)
0428 {
0429 NVTX_YIELD();
0430 NVTX_MEMBAR();
0431 }
0432 }
0433 }
0434
0435 #ifdef __cplusplus
0436 }
0437 #endif